Commit c8b6af84 authored by Jure's avatar Jure

test(cli): remove add/remove tests

parent 88aae5f2
Pipeline #13049 passed with stages
in 11 minutes and 33 seconds
jest.mock('child_process', () => ({ spawnSync: jest.fn() }))
jest.mock('fs-extra', () => {
const fs = require.requireActual('fs-extra')
fs.writeJsonSync = jest.fn()
fs.ensureFileSync = jest.fn()
return fs
})
jest.mock('../../src/package-management/helpers/', () => {
const helpers = require.requireActual('../../src/package-management/helpers/')
helpers.getDepsFromPackageJson = jest.fn()
return helpers
})
const spawnSpy = require('child_process').spawnSync
const fs = require('fs-extra')
const { getMockArgv } = require('../helpers/')
const runAdd = require('../../cli/add')
const readPkgSpy = require('../../src/package-management/helpers/')
.getDepsFromPackageJson
const writeSpy = fs.writeJsonSync
describe('add', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('requires a component', async () => {
await expect(runAdd(getMockArgv(''))).rejects.toBeInstanceOf(Error)
})
it('calls function to write new component into components.json with correct argument', async () => {
const componentName = 'test-widget'
const fullName = `pubsweet-component-${componentName}`
readPkgSpy
.mockImplementationOnce(() => ({}))
.mockImplementationOnce(() => ({ [fullName]: 'version' }))
await runAdd(getMockArgv({ args: componentName }))
const { calls } = writeSpy.mock
expect(calls).toHaveLength(1)
expect(calls[0][1]).toContain(fullName)
})
it('spawns yarn child process with correct arguments', async () => {
const componentName = 'test-widget'
await runAdd(getMockArgv({ args: componentName }))
const { calls } = spawnSpy.mock
expect(calls).toHaveLength(1)
expect(calls[0][1][1]).toBe(`pubsweet-component-${componentName}`)
})
})
jest.mock('child_process', () => ({ spawnSync: jest.fn() }))
jest.mock('fs-extra', () => {
const fs = require.requireActual('fs-extra')
fs.writeJsonSync = jest.fn()
fs.ensureFileSync = jest.fn()
return fs
})
jest.mock('../../src/package-management/helpers/', () => {
const helpers = require.requireActual('../../src/package-management/helpers/')
helpers.getDepsFromPackageJson = jest.fn()
return helpers
})
const fs = require('fs-extra')
const { getMockArgv } = require('../helpers/')
const runRemove = require('../../cli/remove')
const spawnSpy = require('child_process').spawnSync
const readPkgSpy = require('../../src/package-management/helpers/')
.getDepsFromPackageJson
const writeSpy = fs.writeJsonSync
describe('remove', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('requires a component', async () => {
await expect(runRemove(getMockArgv(''))).rejects.toBeInstanceOf(Error)
})
it('removes component from components.json', async () => {
const componentName = 'blog'
const fullName = `pubsweet-component-${componentName}`
readPkgSpy
.mockImplementationOnce(() => ({ [fullName]: 'version' }))
.mockImplementationOnce(() => ({}))
await runRemove(getMockArgv({ args: componentName }))
const { calls } = writeSpy.mock
expect(calls).toHaveLength(1)
expect(calls[0][1]).not.toContain(fullName)
})
it('spawns yarn child process with correct arguments', async () => {
const componentName = 'test-widget'
await runRemove(getMockArgv({ args: componentName }))
const { calls } = spawnSpy.mock
expect(calls).toHaveLength(1)
expect(calls[0][1][1]).toBe(`pubsweet-component-${componentName}`)
})
})
......@@ -67,42 +67,6 @@ describe('CLI: integration test', () => {
})
})
describe('add and remove', () => {
const componentsFile = path.join(appPath, 'config', 'components.json')
const componentName = 'ink-backend'
let oldComponents
beforeAll(() => {
oldComponents = fs.readJsonSync(componentsFile)
})
it('adds component', () => {
const { stdout, stderr } = runCommandSync({
args: `add ${componentName}`,
cwd: appPath,
stdio: 'pipe',
})
console.log(stdout, stderr)
expect(stdout).toContain('Success: 1 components installed')
const configPostAdd = fs.readJsonSync(componentsFile)
expect(configPostAdd).toEqual(
oldComponents.concat(`pubsweet-component-${componentName}`),
)
})
it('removes component', () => {
const { stdout, stderr } = runCommandSync({
args: `remove ${componentName}`,
cwd: appPath,
stdio: 'pipe',
})
console.log(stdout, stderr)
expect(stdout).toContain('Success: 1 components removed')
const configPostRemove = fs.readJsonSync(componentsFile)
expect(configPostRemove).toEqual(oldComponents)
})
})
describe('setupdb', () => {
it('creates tables', () => {
const { stdout, stderr } = runCommandSync({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment