|
1 | 1 | import { Parser } from '../../parser'; |
2 | 2 | import * as path from 'path'; |
3 | | -import { beforeEach, expect, test } from '@jest/globals'; |
4 | | - |
5 | | -// You can import and use all API from the 'vscode' module |
6 | | -// as well as import your extension to test it |
7 | | -import * as vscode from 'vscode' |
8 | | -// const myExtension = require('../extension'); |
| 3 | +import { beforeAll, expect, test } from '@jest/globals'; |
9 | 4 |
|
10 | 5 | describe('Parser Test Suite', () => { |
11 | 6 | let parser, tree, file; |
12 | 7 |
|
13 | | - // UNPARSED TREE TEST |
14 | | - describe('It initializes correctly', () => { |
15 | | - beforeEach(() => { |
16 | | - // Assign the test file and make new instance of Parser |
17 | | - file = path.join(__dirname, '../test_cases/tc_0/index.js'); |
18 | | - // file = path.join(__dirname, '../../../src/test/test_apps/test_0/index.js'); |
| 8 | + // TEST 11: PARSER DOESN'T BREAK UPON RECURSIVE COMPONENTS |
| 9 | + describe('It should render the second call of mutually recursive components, but no further', () => { |
| 10 | + beforeAll(() => { |
| 11 | + file = path.join(__dirname, '../../../../src/test/test_cases/tc_11/index.js'); |
19 | 12 | parser = new Parser(file); |
| 13 | + tree = parser.parse(); |
20 | 14 | }); |
21 | 15 |
|
22 | | - test('It instantiates an object for the parser class', () => { |
23 | | - expect((parser)).toBeInstanceOf(Parser); |
| 16 | + test('Tree should not be undefined', () => { |
| 17 | + expect(tree).toBeDefined(); |
24 | 18 | }); |
25 | 19 |
|
26 | | - test('It begins with a suitable entry file and a tree that is not yet defined', () => { |
27 | | - expect(parser.entryFile).toEqual(file); |
28 | | - expect(tree).toBeUndefined(); |
| 20 | + test('Tree should have an index component while child App1, grandchild App2, great-grandchild App1', () => { |
| 21 | + expect(tree).toHaveProperty('name', 'index'); |
| 22 | + expect(tree.children).toHaveLength(1); |
| 23 | + expect(tree.children[0]).toHaveProperty('name', 'App1'); |
| 24 | + expect(tree.children[0].children).toHaveLength(1); |
| 25 | + expect(tree.children[0].children[0]).toHaveProperty('name', 'App2'); |
| 26 | + expect(tree.children[0].children[0].children).toHaveLength(1); |
| 27 | + expect(tree.children[0].children[0].children[0]).toHaveProperty('name', 'App1'); |
| 28 | + expect(tree.children[0].children[0].children[0].children).toHaveLength(0); |
29 | 29 | }); |
30 | 30 | }); |
31 | 31 |
|
32 | | - // TEST 0: ONE CHILD |
33 | | - describe('It works for simple apps', () => { |
34 | | - beforeEach(() => { |
35 | | - file = path.join(__dirname, ''); |
| 32 | + // TEST 12A: NEXT.JS APPS (pages router) |
| 33 | + describe('It should parse Next.js applications using Pages Router', () => { |
| 34 | + beforeAll(() => { |
| 35 | + file = path.join(__dirname, '../../../../src/test/test_cases/tc_12a/pages/index.js'); |
36 | 36 | parser = new Parser(file); |
37 | 37 | tree = parser.parse(); |
38 | 38 | }); |
39 | 39 |
|
40 | | - test('It returns an defined object tree when parsed', () => { |
41 | | - expect(tree).toBeDefined(); |
42 | | - //expect(tree).toMatchObject() |
| 40 | + test('Root should be named index, children should be named Head and Navbar, children of Navbar should be named Link and Image', () => { |
| 41 | + expect(tree).toHaveProperty('name', 'index'); |
| 42 | + expect(tree.children).toHaveLength(2); |
| 43 | + expect(tree.children[0]).toHaveProperty('name', 'Head'); |
| 44 | + expect(tree.children[1]).toHaveProperty('name', 'Navbar'); |
| 45 | + |
| 46 | + expect(tree.children[1].children).toHaveLength(2); |
| 47 | + expect(tree.children[1].children[0]).toHaveProperty('name', 'Link'); |
| 48 | + expect(tree.children[1].children[1]).toHaveProperty('name', 'Image'); |
43 | 49 | }); |
| 50 | + }); |
44 | 51 |
|
45 | | - // test('Parsed tree has a property called name with value index and one child with name App', () => { |
| 52 | + // TEST 12B: NEXT.JS APPS (app router) |
| 53 | + describe('It should parser Next.js applications using Apps Router', () => { |
| 54 | + beforeAll(() => { |
| 55 | + file = path.join(__dirname, '../../../../src/test/test_cases/tc_12b/app/page.jsx'); |
| 56 | + parser = new Parser(file); |
| 57 | + tree = parser.parse(); |
| 58 | + }); |
46 | 59 |
|
47 | | - // }); |
| 60 | + test('Root should be named page, it should have one child named Homepage', () => { |
| 61 | + expect(tree).toHaveProperty('name', 'page'); |
| 62 | + expect(tree.children).toHaveLength(1); |
| 63 | + expect(tree.children[0]).toHaveProperty('name', 'HomePage'); |
| 64 | + }); |
48 | 65 | }); |
| 66 | + |
| 67 | + // TEST 13: VARIABLE DECLARATION IMPORTS AND REACT.LAZY IMPORTS |
| 68 | + describe('It should parse VariableDeclaration imports including React.lazy imports', () => { |
| 69 | + beforeAll(() => { |
| 70 | + file = path.join(__dirname, '../../../../src/test/test_cases/tc_13/index.js'); |
| 71 | + parser = new Parser(file); |
| 72 | + tree = parser.parse(); |
| 73 | + }); |
49 | 74 |
|
50 | | - // these are the 14 tests we need to test for |
51 | | - |
52 | | - // TEST 1: NESTED CHILDREN |
53 | | - // TEST 2: THIRD PARTY, REACT ROUTER, DESTRUCTURED IMPORTS |
54 | | - // TEST 3: IDENTIFIES REDUX STORE CONNECTION |
55 | | - // TEST 4: ALIASED IMPORTS |
56 | | - // TEST 5: MISSING EXTENSIONS AND UNUSED IMPORTS |
57 | | - // TEST 6: BAD IMPORT OF APP2 FROM APP1 COMPONENT |
58 | | - // TEST 7: SYNTAX ERROR IN APP FILE CAUSES PARSER ERROR |
59 | | - // TEST 8: MULTIPLE PROPS ON ONE COMPONENT |
60 | | - // TEST 9: FINDING DIFFERENT PROPS ACROSS TWO OR MORE IDENTICAL COMPONENTS |
61 | | - // TEST 10: CHECK CHILDREN WORKS AND COMPONENTS WORK |
62 | | - // TEST 11: PARSER DOESN'T BREAK UPON RECURSIVE COMPONENTS |
63 | | - // TEST 12: NEXT.JS APPS (pages version & app router version) |
64 | | - // TEST 13: Variable Declaration Imports and React.lazy Imports |
65 | | - // TEST 14: CHECK IF COMPONENT IS CLIENT OR SERVER (USING HOOKS & DIRECTIVES) => BOOLEAN (priority) |
| 75 | + test('Root should be named index, it should have one child named App', () => { |
| 76 | + expect(tree).toHaveProperty('name', 'index'); |
| 77 | + expect(tree.children).toHaveLength(1); |
| 78 | + expect(tree.children[0]).toHaveProperty('name', 'App'); |
| 79 | + }); |
66 | 80 |
|
67 | | - // LOU is doing EXTENSION TEST in extension.test.ts |
| 81 | + test('App should have three children, Component1, Component2 and Component3, all found successfully', () => { |
| 82 | + expect(tree.children[0].children[0]).toHaveProperty('name', 'Component1'); |
| 83 | + expect(tree.children[0].children[0]).toHaveProperty('thirdParty', false); |
| 84 | + |
| 85 | + expect(tree.children[0].children[1]).toHaveProperty('name', 'Component2'); |
| 86 | + expect(tree.children[0].children[1]).toHaveProperty('thirdParty', false); |
| 87 | + |
| 88 | + expect(tree.children[0].children[2]).toHaveProperty('name', 'Component3'); |
| 89 | + expect(tree.children[0].children[2]).toHaveProperty('thirdParty', false); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + // TEST 14: CHECK IF COMPONENT IS A CLIENT COMPONENT USING HOOKS AND DIRECTIVES |
| 94 | + describe('It should parse components and determine if the component type', () => { |
| 95 | + beforeAll(() => { |
| 96 | + file = path.join(__dirname, '../../../../src/test/test_cases/tc_14/index.js'); |
| 97 | + parser = new Parser(file); |
| 98 | + tree = parser.parse(); |
| 99 | + }); |
| 100 | + |
| 101 | + test('Root should be named index, it should have one children named App', () => { |
| 102 | + expect(tree).toHaveProperty('name', 'index'); |
| 103 | + expect(tree.children).toHaveLength(1); |
| 104 | + expect(tree.children[0]).toHaveProperty('name', 'App'); |
| 105 | + }); |
| 106 | + |
| 107 | + test('App should have three children, Component1 is a client component using hooks, Component2 is a client component using directives, and Component3 is not a client component', () => { |
| 108 | + // expect(tree.children[0].children[0]).toHaveProperty('name', 'Component1'); |
| 109 | + // expect(tree.children[0].children[0]).toHaveProperty('isClientComponent', true); |
| 110 | + |
| 111 | + expect(tree.children[0].children[1]).toHaveProperty('name', 'Component2'); |
| 112 | + expect(tree.children[0].children[1]).toHaveProperty('isClientComponent', true); |
| 113 | + |
| 114 | + expect(tree.children[0].children[2]).toHaveProperty('name', 'Component3'); |
| 115 | + expect(tree.children[0].children[2]).toHaveProperty('isClientComponent', false); |
| 116 | + }); |
| 117 | + }); |
68 | 118 | }); |
0 commit comments