Skip to content

Commit 5658483

Browse files
committed
test: implement tests for collectGrants() util
Also, fix indentation in test file to align with the rest of the repository.
1 parent a64b030 commit 5658483

File tree

1 file changed

+68
-14
lines changed

1 file changed

+68
-14
lines changed

test/util.test.ts

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,77 @@
1+
import { parse as parseCode } from '@babel/parser';
12
import type { AstNode } from 'rollup';
2-
import type { EmptyStatement } from 'estree';
33

44
import {
5-
collectGmApi,
5+
collectGrants,
66
getMetadata
77
} from '../src/util';
88

9-
describe('collectGmApi', () => {
10-
const EMPTY_STATEMENT: EmptyStatement = {
11-
type: 'EmptyStatement'
12-
};
13-
14-
it('should return an empty set on an empty input', () => {
15-
expect(collectGmApi(EMPTY_STATEMENT as AstNode).size).toBe(0);
16-
});
9+
describe('collectGrants', () => {
10+
const parseCodeAsEstreeAst = (code: string) => {
11+
const file = parseCode(code, { plugins: ['estree'] });
12+
return file.program as AstNode;
13+
};
14+
15+
it('should return an empty set on an empty input', () => {
16+
const astNode = parseCodeAsEstreeAst(``);
17+
const result = collectGrants(astNode);
18+
19+
expect(result.size).toBe(0);
20+
});
21+
22+
it('should return only GM_dummyApi', () => {
23+
const astNode = parseCodeAsEstreeAst(`GM_dummyApi`);
24+
const result = collectGrants(astNode);
25+
26+
expect(result.size).toBe(1);
27+
expect(result).toContain('GM_dummyApi');
28+
});
29+
30+
it('should ignore any scope-defined variables that look like GM APIs', () => {
31+
const astNode = parseCodeAsEstreeAst(`
32+
let GM_dummyApi;
33+
GM_dummyApi;
34+
`);
35+
const result = collectGrants(astNode);
36+
37+
expect(result.size).toBe(0);
38+
});
39+
40+
it('should return only GM.dummyApi', () => {
41+
const astNode = parseCodeAsEstreeAst(`GM.dummyApi`);
42+
const result = collectGrants(astNode);
43+
44+
expect(result.size).toBe(1);
45+
expect(result).toContain('GM.dummyApi');
46+
});
47+
48+
it('should return unsafeWindow when presented with just unsafeWindow', () => {
49+
const astNode = parseCodeAsEstreeAst(`unsafeWindow`);
50+
const result = collectGrants(astNode);
51+
52+
expect(result.size).toBe(1);
53+
expect(result).toContain('unsafeWindow');
54+
});
55+
56+
it('should return unsafeWindow even when a subfield is accessed', () => {
57+
const astNode = parseCodeAsEstreeAst(`unsafeWindow.anotherThing`);
58+
const result = collectGrants(astNode);
59+
60+
expect(result.size).toBe(1);
61+
expect(result).toContain('unsafeWindow');
62+
});
63+
64+
it('should return unsafeWindow even when a subfield is accessed with object notation', () => {
65+
const astNode = parseCodeAsEstreeAst(`unsafeWindow["anotherThing"]`);
66+
const result = collectGrants(astNode);
67+
68+
expect(result.size).toBe(1);
69+
expect(result).toContain('unsafeWindow');
70+
});
1771
});
1872

1973
describe('getMetadata', () => {
20-
it('should throw error on an empty input', () => {
21-
expect(() => getMetadata('', new Set())).toThrow(Error);
22-
});
23-
});
74+
it('should throw error on an empty input', () => {
75+
expect(() => getMetadata('', new Set())).toThrow(Error);
76+
});
77+
});

0 commit comments

Comments
 (0)