Skip to content

Commit 92ce8bd

Browse files
committed
✅ add test for header tags
1 parent 83e8fa2 commit 92ce8bd

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

test/integration/headers/fixtures.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
import { ValueType } from 'webpack-userscript';
2+
13
import { File, GlobalFixtures } from '../fixtures';
24

5+
interface TagSample {
6+
value: ValueType;
7+
expect: string;
8+
}
9+
10+
interface TagCase {
11+
validValues: TagSample[];
12+
invalidValues: Omit<TagSample, 'expect'>[];
13+
}
14+
315
export class Fixtures extends GlobalFixtures {
416
public static readonly customValue = '__custom__';
517

@@ -8,4 +20,11 @@ export class Fixtures extends GlobalFixtures {
820

921
@File(__dirname, 'tag-order-headers.txt')
1022
public static readonly tagOrderHeaders: string;
23+
24+
public static readonly tagSamples: Record<string, TagCase> = {
25+
'run-at': {
26+
validValues: [{ value: 'document-body', expect: 'document-body' }],
27+
invalidValues: [{ value: 'a' }],
28+
},
29+
};
1130
}

test/integration/headers/index.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,54 @@ describe('headers', () => {
108108

109109
return expect(promise).toReject();
110110
});
111+
112+
for (const [tag, { validValues, invalidValues }] of Object.entries(
113+
Fixtures.tagSamples,
114+
)) {
115+
describe(tag, () => {
116+
for (const { value, expect: expectedOutput } of validValues) {
117+
it('valid', async () => {
118+
const output = await compile(input, {
119+
...Fixtures.webpackConfig,
120+
plugins: [
121+
new UserscriptPlugin({
122+
headers: {
123+
[tag]: value,
124+
},
125+
}),
126+
],
127+
});
128+
129+
const userJs = output
130+
.readFileSync('/dist/output.user.js')
131+
.toString('utf-8');
132+
const metaJs = output
133+
.readFileSync('/dist/output.meta.js')
134+
.toString('utf-8');
135+
136+
expect(findTags(tag, expectedOutput, userJs)).toHaveLength(1);
137+
expect(findTags(tag, expectedOutput, metaJs)).toHaveLength(1);
138+
});
139+
}
140+
141+
for (const { value } of invalidValues) {
142+
it('invalid', () => {
143+
const promise = compile(input, {
144+
...Fixtures.webpackConfig,
145+
plugins: [
146+
new UserscriptPlugin({
147+
headers: {
148+
[tag]: value,
149+
},
150+
}),
151+
],
152+
});
153+
154+
return expect(promise).toReject();
155+
});
156+
}
157+
});
158+
}
111159
});
112160
});
113161

@@ -121,6 +169,7 @@ describe('headers', () => {
121169
resource: {
122170
test: 'http://example.com/demo.jpg',
123171
},
172+
include: ['https://example.com/', 'http://example.com/'],
124173
noframes: true,
125174
unwrap: false,
126175
},

test/integration/headers/pretty-headers.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// @name userscript
33
// @description this is a fantastic userscript
44
// @version 0.0.0
5-
// @match *://*/*
5+
// @include https://example.com/
6+
// @include http://example.com/
67
// @noframes
78
// @resource test http://example.com/demo.jpg
89
// ==/UserScript==

0 commit comments

Comments
 (0)