/
preeval.test.ts
93 lines (74 loc) · 2.04 KB
/
preeval.test.ts
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
import { join } from 'path';
import { transformAsync } from '@babel/core';
import dedent from 'dedent';
import serializer from '../../__utils__/linaria-snapshot-serializer';
import type { StrictOptions } from '../../babel/types';
expect.addSnapshotSerializer(serializer);
const options: Partial<StrictOptions> = {
displayName: true,
evaluate: true,
};
const transpile = async (input: string) =>
(await transformAsync(input, {
babelrc: false,
presets: [[require.resolve('../../babel/evaluators/preeval'), options]],
plugins: [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-syntax-jsx',
],
filename: join(__dirname, 'app/index.js'),
configFile: false,
}))!;
it('preserves classNames', async () => {
const { code } = await transpile(
dedent`
import { styled } from 'linaria/react';
const Component = styled.div\`\`;
`
);
expect(code).toMatchSnapshot();
});
it('handles locally named import', async () => {
const { code } = await transpile(
dedent`
import { styled as custom } from 'linaria/react';
const Component = custom.div\`\`;
`
);
expect(code).toMatchSnapshot();
});
it('replaces functional component', async () => {
const div = '<div>{props.children}</div>';
const { code } = await transpile(
dedent`
import React from 'react';
const Component = (props) => ${div};
`
);
expect(code).toMatchSnapshot();
});
it('replaces class component', async () => {
const div = '<div>{props.children}</div>';
const { code } = await transpile(
dedent`
import React from 'react';
class Component extends React.PureComponent {
render() {
return ${div};
}
}
`
);
expect(code).toMatchSnapshot();
});
it('replaces constant', async () => {
const div = '<div>{props.children}</div>';
const { code } = await transpile(
dedent`
import React from 'react';
const tag = ${div};
const Component = (props) => tag;
`
);
expect(code).toMatchSnapshot();
});