1 | const babel = require('@babel/core');
|
2 | const assert = require('assert');
|
3 |
|
4 | let reactPlugin;
|
5 | let transform;
|
6 |
|
7 | if (process.env.COVERAGE) {
|
8 | reactPlugin = require('../lib-cov/index').default;
|
9 | } else {
|
10 | reactPlugin = require('../src/index').default;
|
11 | }
|
12 |
|
13 | const somePluginEnter = ({ types: t }) => ({
|
14 | visitor: {
|
15 | Program(path) {
|
16 | path.unshiftContainer('body', t.importDeclaration([
|
17 | t.importDefaultSpecifier(t.identifier('React')),
|
18 | ], t.stringLiteral('react')));
|
19 | },
|
20 | },
|
21 | });
|
22 |
|
23 | const somePluginExit = ({ types: t }) => ({
|
24 | visitor: {
|
25 | Program: {
|
26 | exit(path) {
|
27 | path.unshiftContainer('body', t.importDeclaration([
|
28 | t.importDefaultSpecifier(t.identifier('React')),
|
29 | ], t.stringLiteral('react')));
|
30 | },
|
31 | },
|
32 | },
|
33 | });
|
34 |
|
35 | const somePluginThatCrawl = () => ({
|
36 | visitor: {
|
37 | Program(path) {
|
38 | path.scope.crawl();
|
39 | },
|
40 | },
|
41 | });
|
42 |
|
43 | const somePluginCrazy = () => ({
|
44 | visitor: {
|
45 | Program(_, { file }) {
|
46 | file.get('ourPath').remove();
|
47 | },
|
48 | },
|
49 | });
|
50 |
|
51 | const genericInput = 'export default class Component {render() {return <div />}}';
|
52 | const genericOutput = 'import React from "react";\nexport default class Component {\n render() {\n return <div />;\n }\n\n}';
|
53 |
|
54 | describe('babel-plugin-react', () => {
|
55 | beforeEach(() => {
|
56 | transform = (code, pluginsBefore = [], pluginsAfter = []) => babel.transform(code, {
|
57 | plugins: ['@babel/plugin-syntax-jsx', ...pluginsBefore, reactPlugin, ...pluginsAfter],
|
58 | }).code;
|
59 | });
|
60 |
|
61 | it('should return transpiled code with required React', () => {
|
62 | const transformed = transform(genericInput);
|
63 |
|
64 | assert.equal(transformed, genericOutput);
|
65 | });
|
66 |
|
67 | it('should return not transpiled code', () => {
|
68 | const transformed = transform('console.log("hello world")');
|
69 |
|
70 | assert.equal(transformed, 'console.log("hello world");');
|
71 | });
|
72 |
|
73 | it('should check that plugin does not import React twice', () => {
|
74 | const transformed = transform('class Component{render(){return <div/>}} class Component2{render(){return <div />}}');
|
75 |
|
76 | assert.equal(transformed, 'import React from "react";\n\nclass Component {\n render() {\n return <div />;\n }\n\n}\n\n'
|
77 | + 'class Component2 {\n render() {\n return <div />;\n }\n\n}');
|
78 | });
|
79 |
|
80 | it('should does not replace users import on plugins import', () => {
|
81 | const transformed = transform('import React from"react/addons"\nclass Component{render(){return <div/>}}');
|
82 |
|
83 | assert.equal(transformed, 'import React from "react/addons";\n\nclass Component {\n render() {\n return <div />;\n }\n\n}');
|
84 | });
|
85 |
|
86 | it('should get along with other plugins which add React import', () => {
|
87 | assert.equal(transform(genericInput, [somePluginEnter]), genericOutput);
|
88 | assert.equal(transform(genericInput, [somePluginExit]), genericOutput);
|
89 | assert.equal(transform(genericInput, [], [somePluginEnter]), genericOutput);
|
90 | assert.equal(transform(genericInput, [], [somePluginExit]), genericOutput);
|
91 | });
|
92 |
|
93 | it('should work with other plugins which use scope.crawl on files which already contains React import', () => {
|
94 | const transformed = transform('import * as React from "react";', [], [somePluginThatCrawl]);
|
95 |
|
96 | assert.equal(transformed, 'import * as React from "react";');
|
97 | });
|
98 |
|
99 | it('should not blow up if another plugin removes our import', () => {
|
100 | assert.equal(transform(genericInput, [], [somePluginCrazy]), 'export default class Component {\n render() {\n return <div />;\n }\n\n}');
|
101 | });
|
102 |
|
103 | it('should support JSX fragments', () => {
|
104 | const transformed = transform('function Thing() {\n return <>Hi</>;\n}');
|
105 |
|
106 | assert.equal(transformed, 'import React from "react";\n\nfunction Thing() {\n return <>Hi</>;\n}');
|
107 | });
|
108 | });
|