-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.test.ts
51 lines (37 loc) · 1.9 KB
/
index.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
import path from 'path';
import fs from 'fs';
import * as babel from '@babel/core';
import {expect, test} from 'vitest';
import plugin from '../index';
const HOOK_MODULE = path.join(__dirname, '..', 'useComponentFile.js');
const BABEL_OPTIONS = {
presets: ['@babel/preset-react'],
plugins: [
[
plugin,
{
srcDirectory: path.join(__dirname, 'fixtures', 'src'),
},
],
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-classes',
],
};
const testFixture = (name: string, shouldInject: boolean) => {
const filename = path.join(__dirname, 'fixtures', name);
const content = fs.readFileSync(filename, 'utf-8');
const result = babel.transformSync(content, {...BABEL_OPTIONS, filename});
expect((result?.code ?? '').includes('useComponentFile("')).toBe(shouldInject);
expect((result?.code ?? '').includes(`import useComponentFile from "${HOOK_MODULE}"`)).toBe(shouldInject);
};
test('export default component', () => testFixture('src/export-default-component.js', true));
test('multiple components', () => testFixture('src/multiple-components.js', true));
test('no component with pascal case name', () => testFixture('src/no-component.js', false));
test('simple function', () => testFixture('src/simple-function.js', false));
test('call createElement', () => testFixture('src/create-element.js', true));
test('call React.createElement', () => testFixture('src/react-create-element.js', true));
test('call cloneElement', () => testFixture('src/clone-element.js', true));
test('call React.cloneElement', () => testFixture('src/react-clone-element.js', true));
test('has more than 1 parameters', () => testFixture('src/multiple-parameters.js', false));
test.only('class component', () => testFixture('src/class-component.js', false));
test('outside src directory', () => testFixture('outside-src.js', false));