-
Notifications
You must be signed in to change notification settings - Fork 820
Expand file tree
/
Copy pathtests.ts
More file actions
166 lines (148 loc) · 6.85 KB
/
Copy pathtests.ts
File metadata and controls
166 lines (148 loc) · 6.85 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Severity } from '@hint/utils-types';
import { generateHTMLPage } from '@hint/utils-create-server';
import { getHintPath, HintTest, testHint } from '@hint/utils-tests-helpers';
const hintPath = getHintPath(__filename);
const metaElementIsNotInHeadErrorMessage = `The 'theme-color' meta element should be specified in the '<head>', not '<body>'.`;
const metaElementIsNotNeededErrorMessage = `A 'theme-color' meta element is not needed as one was already specified.`;
const metaElementIsNotSpecifiedErrorMessage = `A 'theme-color' meta element was not specified.`;
const metaElementHasIncorrectNameAttributeErrorMessage = `The 'theme-color' meta element 'name' attribute value should be 'theme-color'.`;
const invalidColorValues = [
'currentcolor',
'invalid'
];
const notAlwaysSupportedColorValues = [
'#f00a',
'#ff0000aa'
];
const unsupportedColorValues = [
'hwb(60, 3%, 60%)'
];
const validColorValues = [
'#f00',
'#fF0000',
'hsl(5, 5%, 5%)',
'hsla(5, 5%, 5%, 0.5)',
'red',
'rgb(5, 5, 5)',
'rgba(5, 5, 5, 0.5)',
'transparent'
];
const generateThemeColorMetaElement = (contentValue = '#f00', nameValue = 'theme-color', mediaValue?: string) => {
const mediaAttr = mediaValue ? ` media="${mediaValue}"` : '';
return `<meta name="${nameValue}" content="${contentValue}"${mediaAttr}>`;
};
const generateTest = (colorValues: string[], valueType = 'valid', reason?: string) => {
const defaultTests = [];
for (const colorValue of colorValues) {
const test: HintTest = {
name: `'theme-color' meta element is specified with ${valueType} 'content' value of '${colorValue}'${typeof reason === 'undefined' ? '' : ` ${reason}`}`,
serverConfig: generateHTMLPage(generateThemeColorMetaElement(colorValue))
};
if (valueType === 'invalid') {
test.reports = [{ message: `The 'theme-color' meta element 'content' attribute should have a valid color value.` }];
} else if (valueType === 'unsupported') {
test.reports = [{ message: `The 'theme-color' meta element 'content' attribute uses a color value not supported by all target browsers.` }];
}
defaultTests.push(test);
}
return defaultTests;
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const defaultTests: HintTest[] = [
{
name: `'theme-color' meta element is not specified, but there is no manifest`,
serverConfig: generateHTMLPage('<link>')
},
{
name: `'theme-color' meta element is not specified`,
reports: [{
message: metaElementIsNotSpecifiedErrorMessage,
severity: Severity.warning
}],
serverConfig: generateHTMLPage('<link rel="manifest" href="manifest.webmanifest">')
},
{
name: `'theme-color' meta element is specified with invalid 'name' value`,
reports: [{
message: metaElementHasIncorrectNameAttributeErrorMessage,
severity: Severity.warning
}],
serverConfig: generateHTMLPage(generateThemeColorMetaElement('#f00', ' thEme-color '))
},
...generateTest([...validColorValues, ...notAlwaysSupportedColorValues]),
...generateTest(invalidColorValues, 'invalid'),
...generateTest(unsupportedColorValues, 'unsupported'),
{
name: `'theme-color' meta element is specified in the '<body>'`,
reports: [{
message: metaElementIsNotInHeadErrorMessage,
severity: Severity.error
}],
serverConfig: generateHTMLPage(undefined, generateThemeColorMetaElement())
},
{
name: `Multiple meta 'theme-color' elements are specified`,
reports: [{
message: metaElementIsNotNeededErrorMessage,
severity: Severity.warning
}],
serverConfig: generateHTMLPage(`${generateThemeColorMetaElement()}${generateThemeColorMetaElement()}`)
},
{
name: `Target is not served with a valid media type`,
serverConfig: { '/': { headers: { 'Content-Type': 'invalid' } } }
},
{
name: `Target is served with a non-HTML specific media type`,
serverConfig: { '/': { headers: { 'Content-Type': 'application/javascript; charset=utf-8' } } }
}
];
// New tests for multiple theme-color elements with media attributes
const mediaAttributeTests: HintTest[] = [
{
name: `Multiple 'theme-color' elements with different media attributes should pass`,
serverConfig: generateHTMLPage(`${generateThemeColorMetaElement('#f00', 'theme-color', '(prefers-color-scheme: light)')}${generateThemeColorMetaElement('#333', 'theme-color', '(prefers-color-scheme: dark)')}`)
},
{
name: `Multiple 'theme-color' elements with same media attribute should fail`,
reports: [{
message: metaElementIsNotNeededErrorMessage,
severity: Severity.warning
}],
serverConfig: generateHTMLPage(`${generateThemeColorMetaElement('#f00', 'theme-color', '(prefers-color-scheme: light)')}${generateThemeColorMetaElement('#333', 'theme-color', '(prefers-color-scheme: light)')}`)
},
{
name: `One 'theme-color' without media and one with media should pass`,
serverConfig: generateHTMLPage(`${generateThemeColorMetaElement('#f00')}${generateThemeColorMetaElement('#333', 'theme-color', '(prefers-color-scheme: dark)')}`)
},
{
name: `Multiple 'theme-color' elements without media attributes should fail`,
reports: [{
message: metaElementIsNotNeededErrorMessage,
severity: Severity.warning
}],
serverConfig: generateHTMLPage(`${generateThemeColorMetaElement('#f00')}${generateThemeColorMetaElement('#333')}`)
},
{
name: `Multiple 'theme-color' elements with various different media attributes should pass`,
serverConfig: generateHTMLPage(`${generateThemeColorMetaElement('#f00', 'theme-color', '(prefers-color-scheme: light)')}${generateThemeColorMetaElement('#333', 'theme-color', '(prefers-color-scheme: dark)')}${generateThemeColorMetaElement('#666', 'theme-color', '(prefers-contrast: high)')}`)
},
{
name: `Issue #6001 scenario: theme-color with prefers-color-scheme dark and light should pass`,
serverConfig: generateHTMLPage(`${generateThemeColorMetaElement('#13171a', 'theme-color', '(prefers-color-scheme: dark)')}${generateThemeColorMetaElement('#f8f4f0', 'theme-color', '(prefers-color-scheme: light)')}`)
}
];
const testForNoSupportForHexWithAlpha: HintTest[] = [...generateTest(notAlwaysSupportedColorValues, 'unsupported', 'because of the targeted browsers')];
testHint(hintPath, defaultTests, {
browserslist: [
'chrome 65',
'firefox 60'
]
});
testHint(hintPath, mediaAttributeTests, {
browserslist: [
'chrome 65',
'firefox 60'
]
});
testHint(hintPath, testForNoSupportForHexWithAlpha, { browserslist: ['chrome 50'] });