-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathcall.test.js
114 lines (92 loc) · 3.47 KB
/
call.test.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { transform } from '@babel/core';
import plugin from '../src';
// all calls take a path as the first argument
const calls = [
'require',
'require.resolve',
'System.import',
'jest.genMockFromModule',
'jest.mock',
'jest.unmock',
'jest.doMock',
'jest.dontMock',
'jest.setMock',
'jest.requireActual',
'jest.requireMock',
'require.requireActual',
'require.requireMock',
];
describe('function and method calls', () => {
const transformerOpts = {
babelrc: false,
plugins: [
[
plugin,
{
root: './test/testproject/src',
alias: {
test: './test/testproject/test',
},
},
],
],
};
calls.forEach(name => {
describe(name, () => {
it('should resolve the path based on the root config', () => {
const code = `${name}("components/Header/SubHeader", ...args);`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(
`${name}("./test/testproject/src/components/Header/SubHeader", ...args);`
);
});
it('should alias the path', () => {
const code = `${name}("test", ...args);`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(`${name}("./test/testproject/test", ...args);`);
});
it('should not change a relative path', () => {
const code = `${name}("./utils", ...args);`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(`${name}("./utils", ...args);`);
});
it('should handle no arguments', () => {
const code = `${name}();`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(`${name}();`);
});
it('should handle the first argument not being a string literal', () => {
const code = `${name}(path, ...args);`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(`${name}(path, ...args);`);
});
it('should handle an empty path', () => {
const code = `${name}('', ...args);`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(`${name}('', ...args);`);
});
it('should ignore the call if the method name is not fully matched (suffix)', () => {
const code = `${name}.after("components/Sidebar/Footer", ...args);`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(`${name}.after("components/Sidebar/Footer", ...args);`);
});
it('should ignore the call if the method name is not fully matched (prefix)', () => {
const code = `before.${name}("components/Sidebar/Footer", ...args);`;
const result = transform(code, transformerOpts);
expect(result.code).toBe(`before.${name}("components/Sidebar/Footer", ...args);`);
});
});
});
it('should resolve the path if the method name is a string literal', () => {
const code = 'require["resolve"]("components/Sidebar/Footer", ...args);';
const result = transform(code, transformerOpts);
expect(result.code).toBe(
'require["resolve"]("./test/testproject/src/components/Sidebar/Footer", ...args);'
);
});
it('should ignore the call if the method name is unknown', () => {
const code = 'unknown("components/Sidebar/Footer", ...args);';
const result = transform(code, transformerOpts);
expect(result.code).toBe('unknown("components/Sidebar/Footer", ...args);');
});
});