-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdirective.parser.spec.ts
More file actions
111 lines (101 loc) · 3.02 KB
/
Copy pathdirective.parser.spec.ts
File metadata and controls
111 lines (101 loc) · 3.02 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
108
109
110
111
import fs from 'fs';
import { tsquery } from '@phenomnomnominal/tsquery';
import { NgParselOutputType } from '../shared/model/types.model.js';
import { parseDirective } from './directive.parser.js';
describe('DirectiveParser', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('should extract all the properties from the directive', function () {
const filePath = 'foo.directive.ts';
const implementation = `export class MyTestDirective {
@Input() foo: string;
@Output() bar = new EventEmitter();
public value = signal<string>('');
}`;
const ast = tsquery.ast(`
@Directive({
selector: '[myTestDirective]'
})
${implementation}
`);
const expectedOutput = {
type: NgParselOutputType.DIRECTIVE,
className: 'MyTestDirective',
filePath,
selector: '[myTestDirective]',
standalone: false,
inputs: [
{
decorator: '@Input()',
name: 'foo',
type: 'string',
initializer: undefined,
field: '@Input() foo: string',
},
],
outputs: [
{
decorator: '@Output()',
name: 'bar',
type: undefined,
initializer: 'new EventEmitter()',
field: '@Output() bar = new EventEmitter()',
},
],
implementation,
methodsPublicExplicit: [],
fieldsPublicExplicit: [{
name: 'value',
type: 'inferred',
value: `signal<string>('')`,
}]
};
jest.spyOn(fs, 'readFileSync').mockReturnValue(implementation);
expect(parseDirective(ast, filePath)).toEqual(expectedOutput);
});
it('should extract all the properties from the standalone directive', function () {
const filePath = 'foo.directive.ts';
const implementation = `export class MyTestDirective {
@Input() foo: string;
@Output() bar = new EventEmitter();
}`;
const ast = tsquery.ast(`
@Directive({
selector: '[myTestDirective]',
standalone: true
})
${implementation}
`);
const expectedOutput = {
type: NgParselOutputType.DIRECTIVE,
className: 'MyTestDirective',
filePath,
selector: '[myTestDirective]',
standalone: true,
inputs: [
{
decorator: '@Input()',
name: 'foo',
type: 'string',
initializer: undefined,
field: '@Input() foo: string',
},
],
outputs: [
{
decorator: '@Output()',
name: 'bar',
type: undefined,
initializer: 'new EventEmitter()',
field: '@Output() bar = new EventEmitter()',
},
],
implementation,
methodsPublicExplicit: [],
fieldsPublicExplicit: [],
};
jest.spyOn(fs, 'readFileSync').mockReturnValue(implementation);
expect(parseDirective(ast, filePath)).toEqual(expectedOutput);
});
});