-
-
Notifications
You must be signed in to change notification settings - Fork 318
/
io.js
58 lines (46 loc) · 1.7 KB
/
io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import fetch from 'node-fetch';
import { handleError, toJSONString, write, generate } from '../lib/io.mjs';
import testData from './sampleProperties.json';
jest.mock('node-fetch');
// Selectively bypass mocking to use a real Response obj
const { Response } = jest.requireActual('node-fetch');
describe('toJSONString', () => {
const rawData = {
foo: 'bar',
'baz-qux': 'quux',
quuz: 'corge',
garply: 'waldo',
'fred-plugh': 'xyzzy-thud',
};
test('should produce parsable JSON', () => {
expect(JSON.parse(toJSONString(rawData))).toEqual(rawData);
});
});
describe('Smoke tests', () => {
const data = {
fromInitial: { foo: 'bar', baz: 'qux' },
toInitial: { qux: 'baz', bar: 'foo' },
};
const paths = { fromInitial: '/foo.json', toInitial: '/bar.json' };
test.each([
['fromInitial', paths.fromInitial, data.fromInitial],
['toInitial', paths.toInitial, data.toInitial],
])('should write JSON file based on key (%p)', (key, path, expected) => {
const err = expect.any(Function);
const fileFunc = jest.fn();
write(fileFunc, paths, data, key);
expect(fileFunc).toHaveBeenCalledWith(path, toJSONString(expected), err);
});
test('should handle file operation errors', () => {
expect(handleError).not.toThrowError();
expect(() => handleError(new Error('something went wrong'))).toThrowError();
});
test('should make it through promise chain with sample data and write 2 files', async () => {
const fileFunc = jest.fn();
fetch.mockReturnValue(
Promise.resolve(new Response(JSON.stringify(testData)))
);
await generate(fileFunc, paths, 'https://example.com/properties.json');
expect(fileFunc).toHaveBeenCalledTimes(2);
});
});