Skip to content

Commit

Permalink
Refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
akameco committed Oct 7, 2017
1 parent 51995f7 commit 2d11ee0
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/handlers/__tests__/plugins.test.js
Expand Up @@ -29,6 +29,8 @@ afterEach(() => {
writeSpy.mockRestore()
})

const setup = plugin => [getEventPath(), 'add', plugin]

test('compileWithPlugin', () => {
const result = plugins.compileWithPlugin(getEventPath(), {
test: /dummy/,
Expand Down Expand Up @@ -56,45 +58,45 @@ test('from option works when plugin === Array', () => {
test('handlePlugin when eventPath not match', () => {
const spyFn = jest.spyOn(plugins, 'compileWithPlugin')
const plugin = { test: /hoge/, plugin: _plugin }
plugins.handlePlugin(getEventPath(), 'add', plugin)
plugins.handlePlugin(...setup(plugin))
expect(spyFn.mock.calls.length).toBe(0)
})

test('handlePlugin when eventPath match', () => {
const plugin = { test: /a.js/, plugin: _plugin }
plugins.handlePlugin(getEventPath(), 'add', plugin)
plugins.handlePlugin(...setup(plugin))
expect(stripAnsi(logSpy.mock.calls[0][0])).toMatchSnapshot()
})

test('handlePlugin with input option', () => {
const plugin = { test: /a.js/, plugin: _plugin, input: getEventPath() }
plugins.handlePlugin(getEventPath(), 'add', plugin)
plugins.handlePlugin(...setup(plugin))
expect(writeSpy.mock.calls[0][1]).toMatchSnapshot()
})

test('handlePlugin with relative input path', () => {
const plugin = { test: /a.js/, plugin: _plugin, input: 'b.js' }
plugins.handlePlugin(getEventPath(), 'add', plugin)
plugins.handlePlugin(...setup(plugin))
expect(writeSpy.mock.calls[0][1]).toMatchSnapshot()
})

test('handlePlugin with output option', () => {
const plugin = { test: /a.js/, plugin: _plugin, output: 'b.js' }
plugins.handlePlugin(getEventPath(), 'add', plugin)
plugins.handlePlugin(...setup(plugin))
expect(writeSpy.mock.calls[0][0]).toMatch('/fixtures/b.js')
})

test('handlePlugin write args', () => {
const plugin = { test: /a.js/, plugin: _plugin }
plugins.handlePlugin(getEventPath(), 'add', plugin)
plugins.handlePlugin(...setup(plugin))
expect(writeSpy.mock.calls[0][0]).toBe(getEventPath())
expect(writeSpy.mock.calls[0][1]).toMatchSnapshot()
})

test('handlePlugin when compileWithPlugin returns empty code', () => {
const spy = jest.spyOn(utils, 'compile').mockReturnValue('')
const plugin = { test: /a.js/, plugin: _plugin }
plugins.handlePlugin(getEventPath(), 'add', plugin)
plugins.handlePlugin(...setup(plugin))
expect(logSpy).not.toHaveBeenCalled()
spy.mockRestore()
})
Expand Down Expand Up @@ -127,12 +129,12 @@ test('handlePlugin when plugins = undefined', () => {

test('call write handlePlugin when only === add', () => {
const plugin = { test: /a.js/, plugin: _plugin, only: ['add'] }
plugins.handlePlugin(getEventPath(), 'add', plugin)
plugins.handlePlugin(...setup(plugin))
expect(writeSpy).toHaveBeenCalled()
})

test('not call write handlePlugin when only != add', () => {
const plugin = { test: /a.js/, plugin: _plugin, only: ['unlink'] }
plugins.handlePlugin(getEventPath(), 'add', plugin)
plugins.handlePlugin(...setup(plugin))
expect(writeSpy).not.toHaveBeenCalled()
})

0 comments on commit 2d11ee0

Please sign in to comment.