1
|
|
|
const inputHelper = require('../../helper/input') |
2
|
|
|
|
3
|
|
|
describe('Input helper', () => { |
4
|
|
|
const spy = jest.spyOn(console, 'error').mockImplementation(() => {}) |
|
|
|
|
5
|
|
|
|
6
|
|
|
it('input is a file path', () => { |
7
|
|
|
const mockCallback = jest.fn() |
|
|
|
|
8
|
|
|
inputHelper('src/test/sample.html', mockCallback) |
9
|
|
|
expect(mockCallback.mock.calls.length).toBe(1) |
10
|
|
|
expect(mockCallback.mock.calls[0][0]).toMatchSnapshot() |
11
|
|
|
}) |
12
|
|
|
|
13
|
|
|
it('input is a not existed file path', () => { |
14
|
|
|
const mockCallback = jest.fn() |
|
|
|
|
15
|
|
|
inputHelper('src/test/dummy.html', mockCallback) |
16
|
|
|
expect(spy).toHaveBeenCalled() |
17
|
|
|
expect(mockCallback.mock.calls.length).toBe(0) |
18
|
|
|
expect(spy.mock.calls[0][0]).toContain('Cannot read file') |
19
|
|
|
}) |
20
|
|
|
|
21
|
|
|
it('input is a read stream', done => { |
22
|
|
|
const Readable = require('stream').Readable |
23
|
|
|
const readable = new Readable() |
24
|
|
|
const mockCallback = jest.fn(() => {}) |
|
|
|
|
25
|
|
|
readable._read = () => {} |
26
|
|
|
readable.push('streamData 1') |
27
|
|
|
readable.push('streamData 2') |
28
|
|
|
readable.push('streamData 3') |
29
|
|
|
inputHelper(readable, mockCallback) |
30
|
|
|
setTimeout(() => { |
31
|
|
|
try { |
32
|
|
|
expect(mockCallback.mock.calls[0][0]).toBe('streamData 1') |
33
|
|
|
expect(mockCallback.mock.calls[1][0]).toBe('streamData 2') |
34
|
|
|
expect(mockCallback.mock.calls[2][0]).toBe('streamData 3') |
35
|
|
|
done() |
36
|
|
|
} catch (err) { |
37
|
|
|
done.fail(err) |
38
|
|
|
} |
39
|
|
|
}, 2000) |
40
|
|
|
}) |
41
|
|
|
|
42
|
|
|
it('input is invalid', () => { |
43
|
|
|
inputHelper(null) |
44
|
|
|
expect(spy).toHaveBeenCalled() |
45
|
|
|
expect(spy.mock.calls[1][0]).toContain('Invalid Input') |
46
|
|
|
}) |
47
|
|
|
}) |
48
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.