forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-require-statements.test.js
More file actions
107 lines (88 loc) · 2.76 KB
/
get-require-statements.test.js
File metadata and controls
107 lines (88 loc) · 2.76 KB
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
import { transform } from 'babel-standalone';
import dynamicImportSyntax from 'babel-plugin-syntax-dynamic-import';
import detective from './plugins/babel-plugin-detective';
import importNode from './plugins/babel-plugin-dynamic-import-node';
import getRequireStatements from './get-require-statements';
describe('get-require-statements', () => {
function testAst(code) {
const result = transform(code, {
presets: ['es2015', 'react'],
plugins: [
'transform-async-to-generator',
'transform-object-rest-spread',
'transform-class-properties',
'transform-decorators-legacy',
dynamicImportSyntax,
importNode,
[detective, { source: true, nodes: true }],
],
});
expect(getRequireStatements(detective.metadata(result))).toMatchSnapshot();
}
it('can find simple requires', () => {
const code = `
import React from 'react';
import { render } from 'react-dom';
`;
testAst(code);
});
it('can find plain requires', () => {
const code = `
const react = require('react');
const reactDom = require('react-dom');
`;
testAst(code);
});
it('can find import promises', () => {
const code = `
const reactDom = import('react-dom').then(dom => dom.render('a'));
`;
testAst(code);
});
it('can find dynamic imports', () => {
const code = `
const page = import('./' + this.props.page);
const page2 = require('./page/' + this.props.page2);
`;
testAst(code);
});
it('can find reexports', () => {
const code = `
export * from './Hello';
`;
testAst(code);
});
it('can work with real life code', () => {
const code = `
import React from 'react'
import { connect } from 'react-redux'
import { TransitionGroup, Transition } from 'transition-group'
import universal from 'react-universal-component'
import isLoading from '../selectors/isLoading'
import '../css/Switcher.css'
const Switcher = ({ page, direction, isLoading }) =>
<TransitionGroup
className={\`switcher $\{direction}\`}
duration={500}
prefix='slide'
>
<Transition key={page}>
<UniversalComponent page={page} isLoading={isLoading} />
</Transition>
</TransitionGroup>
const UniversalComponent = universal(props => import(\`./$\{props.page}\`), {
minDelay: 500,
chunkName: props => props.page,
loading: () => <div className='spinner'><div /></div>,
error: () => <div className='notFound'>PAGE NOT FOUND - 404</div>
})
const mapState = ({ page, direction, ...state }) => ({
page,
direction,
isLoading: isLoading(state)
})
export default connect(mapState)(Switcher)
`;
testAst(code);
});
});