Skip to content

Commit 9a25b57

Browse files
committed
Test(validate): add tests for profile option passing in validateText and validateURL
1 parent 1c0f76c commit 9a25b57

7 files changed

+223
-2
lines changed

src/retrieve-validation/build-form-data.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('#buildFormData()', () => {
1717
text: '.foo { color: red; }',
1818
medium: undefined,
1919
warningLevel: undefined,
20+
profile: undefined,
2021
})
2122
).toBe(
2223
`--${boundary}\r\nContent-Disposition: form-data; name="text"\r\n\r\n.foo { color: red; }\r\n--${boundary}\r\nContent-Disposition: form-data; name="profile"\r\n\r\ncss3\r\n--${boundary}\r\nContent-Disposition: form-data; name="output"\r\n\r\napplication/json\r\n--${boundary}\r\nContent-Disposition: form-data; name="usermedium"\r\n\r\nall\r\n--${boundary}\r\nContent-Disposition: form-data; name="warning"\r\n\r\nno\r\n--${boundary}`
@@ -29,9 +30,10 @@ describe('#buildFormData()', () => {
2930
text: '.foo { color: red; }',
3031
medium: 'braille',
3132
warningLevel: 3,
33+
profile: 'css3svg',
3234
})
3335
).toBe(
34-
`--${boundary}\r\nContent-Disposition: form-data; name="text"\r\n\r\n.foo { color: red; }\r\n--${boundary}\r\nContent-Disposition: form-data; name="profile"\r\n\r\ncss3\r\n--${boundary}\r\nContent-Disposition: form-data; name="output"\r\n\r\napplication/json\r\n--${boundary}\r\nContent-Disposition: form-data; name="usermedium"\r\n\r\nbraille\r\n--${boundary}\r\nContent-Disposition: form-data; name="warning"\r\n\r\n2\r\n--${boundary}`
36+
`--${boundary}\r\nContent-Disposition: form-data; name="text"\r\n\r\n.foo { color: red; }\r\n--${boundary}\r\nContent-Disposition: form-data; name="profile"\r\n\r\ncss3svg\r\n--${boundary}\r\nContent-Disposition: form-data; name="output"\r\n\r\napplication/json\r\n--${boundary}\r\nContent-Disposition: form-data; name="usermedium"\r\n\r\nbraille\r\n--${boundary}\r\nContent-Disposition: form-data; name="warning"\r\n\r\n2\r\n--${boundary}`
3537
);
3638
});
3739
});

src/retrieve-validation/build-request-url-parameters.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('#buildRequestURLParameters()', () => {
99
url: 'https://raw.githubusercontent.com/sparksuite/w3c-css-validator/master/public/css/valid.css',
1010
medium: undefined,
1111
warningLevel: undefined,
12+
profile: undefined,
1213
})
1314
).toBe(
1415
'?uri=https%3A%2F%2Fraw.githubusercontent.com%2Fsparksuite%2Fw3c-css-validator%2Fmaster%2Fpublic%2Fcss%2Fvalid.css&usermedium=all&warning=no&output=application/json&profile=css3'
@@ -21,9 +22,10 @@ describe('#buildRequestURLParameters()', () => {
2122
url: 'https://raw.githubusercontent.com/sparksuite/w3c-css-validator/master/public/css/valid.css',
2223
medium: 'braille',
2324
warningLevel: 3,
25+
profile: 'css3svg',
2426
})
2527
).toBe(
26-
'?uri=https%3A%2F%2Fraw.githubusercontent.com%2Fsparksuite%2Fw3c-css-validator%2Fmaster%2Fpublic%2Fcss%2Fvalid.css&usermedium=braille&warning=2&output=application/json&profile=css3'
28+
'?uri=https%3A%2F%2Fraw.githubusercontent.com%2Fsparksuite%2Fw3c-css-validator%2Fmaster%2Fpublic%2Fcss%2Fvalid.css&usermedium=braille&warning=2&output=application/json&profile=css3svg'
2729
);
2830
});
2931
});

src/retrieve-validation/index-browser.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('#retrieveValidation()', () => {
2121
url: 'https://raw.githubusercontent.com/sparksuite/w3c-css-validator/master/public/css/valid.css',
2222
medium: undefined,
2323
warningLevel: undefined,
24+
profile: undefined,
2425
},
2526
3000
2627
);

src/retrieve-validation/index-node.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('#retrieveValidation()', () => {
1414
url: 'https://raw.githubusercontent.com/sparksuite/w3c-css-validator/master/public/css/valid.css',
1515
medium: undefined,
1616
warningLevel: undefined,
17+
profile: undefined,
1718
},
1819
3000
1920
);

src/retrieve-validation/process-parameters.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('#processParameters()', () => {
1616
url: 'https://raw.githubusercontent.com/sparksuite/w3c-css-validator/master/public/css/valid.css',
1717
medium: undefined,
1818
warningLevel: undefined,
19+
profile: undefined,
1920
};
2021

2122
expect(processParameters('GET', parameters)).toBe(buildRequestURLParameters(parameters));
@@ -26,6 +27,7 @@ describe('#processParameters()', () => {
2627
text: '.foo { text-align: center; }',
2728
medium: undefined,
2829
warningLevel: undefined,
30+
profile: undefined,
2931
};
3032

3133
expect(processParameters('POST', parameters)).toBe(buildFormData(parameters));
@@ -36,6 +38,7 @@ describe('#processParameters()', () => {
3638
text: '.foo { text-align: center; }',
3739
medium: undefined,
3840
warningLevel: undefined,
41+
profile: undefined,
3942
};
4043

4144
// @ts-expect-error We are trying to throw an error here
@@ -49,6 +52,7 @@ describe('#processParameters()', () => {
4952
text: '.foo { text-align: center; }',
5053
medium: undefined,
5154
warningLevel: undefined,
55+
profile: undefined,
5256
};
5357

5458
expect(() => processParameters('GET', parameters)).toThrow(
@@ -61,6 +65,7 @@ describe('#processParameters()', () => {
6165
url: 'https://raw.githubusercontent.com/sparksuite/w3c-css-validator/master/public/css/valid.css',
6266
medium: undefined,
6367
warningLevel: undefined,
68+
profile: undefined,
6469
};
6570

6671
expect(() => processParameters('POST', parameters)).toThrow(

src/validate-text.test.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,111 @@ export default function testValidateText(validateText: ValidateText): void {
8787
expect(error.message).not.toMatch(/ : /);
8888
}
8989
});
90+
91+
describe('Options passing', () => {
92+
let mockRetrieveValidation: jest.SpyInstance;
93+
94+
beforeEach(() => {
95+
mockRetrieveValidation = jest.spyOn(require('./retrieve-validation'), 'default').mockImplementation(() => ({
96+
validity: true,
97+
errors: [],
98+
}));
99+
});
100+
101+
afterEach(() => {
102+
mockRetrieveValidation.mockRestore();
103+
});
104+
105+
it('Passes profile option to retrieveValidation', async () => {
106+
const textToBeValidated = '.foo { color: blue; }';
107+
await validateText(textToBeValidated, { profile: 'css21' });
108+
109+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
110+
'POST',
111+
expect.objectContaining({
112+
text: textToBeValidated,
113+
profile: 'css21',
114+
}),
115+
expect.any(Number)
116+
);
117+
});
118+
119+
it('Passes default css3 profile when no profile is specified', async () => {
120+
const textToBeValidated = '.foo { color: blue; }';
121+
await validateText(textToBeValidated);
122+
123+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
124+
'POST',
125+
expect.objectContaining({
126+
text: textToBeValidated,
127+
profile: undefined,
128+
}),
129+
expect.any(Number)
130+
);
131+
});
132+
133+
it('Passes medium option to retrieveValidation', async () => {
134+
const textToBeValidated = '.foo { color: blue; }';
135+
await validateText(textToBeValidated, { medium: 'print' });
136+
137+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
138+
'POST',
139+
expect.objectContaining({
140+
text: textToBeValidated,
141+
medium: 'print',
142+
}),
143+
expect.any(Number)
144+
);
145+
});
146+
147+
it('Passes warningLevel option to retrieveValidation', async () => {
148+
const textToBeValidated = '.foo { color: blue; }';
149+
await validateText(textToBeValidated, { warningLevel: 2 });
150+
151+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
152+
'POST',
153+
expect.objectContaining({
154+
text: textToBeValidated,
155+
warningLevel: 2,
156+
}),
157+
expect.any(Number)
158+
);
159+
});
160+
161+
it('Passes timeout option to retrieveValidation', async () => {
162+
const textToBeValidated = '.foo { color: blue; }';
163+
await validateText(textToBeValidated, { timeout: 5000 });
164+
165+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
166+
'POST',
167+
expect.objectContaining({
168+
text: textToBeValidated,
169+
}),
170+
5000
171+
);
172+
});
173+
174+
it('Passes all options together to retrieveValidation', async () => {
175+
const textToBeValidated = '.foo { color: blue; }';
176+
await validateText(textToBeValidated, {
177+
profile: 'css3svg',
178+
medium: 'screen',
179+
warningLevel: 3,
180+
timeout: 15000,
181+
});
182+
183+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
184+
'POST',
185+
expect.objectContaining({
186+
text: textToBeValidated,
187+
profile: 'css3svg',
188+
medium: 'screen',
189+
warningLevel: 3,
190+
}),
191+
15000
192+
);
193+
});
194+
});
90195
});
91196
}
92197

src/validate-url.test.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,111 @@ export default function testValidateURL(validateURL: ValidateURL): void {
125125
expect(error.message).not.toMatch(/ : /);
126126
}
127127
});
128+
129+
describe('Options passing', () => {
130+
let mockRetrieveValidation: jest.SpyInstance;
131+
132+
beforeEach(() => {
133+
mockRetrieveValidation = jest.spyOn(require('./retrieve-validation'), 'default').mockImplementation(() => ({
134+
validity: true,
135+
errors: [],
136+
}));
137+
});
138+
139+
afterEach(() => {
140+
mockRetrieveValidation.mockRestore();
141+
});
142+
143+
it('Passes profile option to retrieveValidation', async () => {
144+
const urlToBeValidated = 'https://example.com/style.css';
145+
await validateURL(urlToBeValidated, { profile: 'css21' });
146+
147+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
148+
'GET',
149+
expect.objectContaining({
150+
url: urlToBeValidated,
151+
profile: 'css21',
152+
}),
153+
expect.any(Number)
154+
);
155+
});
156+
157+
it('Passes default css3 profile when no profile is specified', async () => {
158+
const urlToBeValidated = 'https://example.com/style.css';
159+
await validateURL(urlToBeValidated);
160+
161+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
162+
'GET',
163+
expect.objectContaining({
164+
url: urlToBeValidated,
165+
profile: undefined,
166+
}),
167+
expect.any(Number)
168+
);
169+
});
170+
171+
it('Passes medium option to retrieveValidation', async () => {
172+
const urlToBeValidated = 'https://example.com/style.css';
173+
await validateURL(urlToBeValidated, { medium: 'print' });
174+
175+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
176+
'GET',
177+
expect.objectContaining({
178+
url: urlToBeValidated,
179+
medium: 'print',
180+
}),
181+
expect.any(Number)
182+
);
183+
});
184+
185+
it('Passes warningLevel option to retrieveValidation', async () => {
186+
const urlToBeValidated = 'https://example.com/style.css';
187+
await validateURL(urlToBeValidated, { warningLevel: 2 });
188+
189+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
190+
'GET',
191+
expect.objectContaining({
192+
url: urlToBeValidated,
193+
warningLevel: 2,
194+
}),
195+
expect.any(Number)
196+
);
197+
});
198+
199+
it('Passes timeout option to retrieveValidation', async () => {
200+
const urlToBeValidated = 'https://example.com/style.css';
201+
await validateURL(urlToBeValidated, { timeout: 5000 });
202+
203+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
204+
'GET',
205+
expect.objectContaining({
206+
url: urlToBeValidated,
207+
}),
208+
5000
209+
);
210+
});
211+
212+
it('Passes all options together to retrieveValidation', async () => {
213+
const urlToBeValidated = 'https://example.com/style.css';
214+
await validateURL(urlToBeValidated, {
215+
profile: 'css3svg',
216+
medium: 'screen',
217+
warningLevel: 3,
218+
timeout: 15000,
219+
});
220+
221+
expect(mockRetrieveValidation).toHaveBeenCalledWith(
222+
'GET',
223+
expect.objectContaining({
224+
url: urlToBeValidated,
225+
profile: 'css3svg',
226+
medium: 'screen',
227+
warningLevel: 3,
228+
}),
229+
15000
230+
);
231+
});
232+
});
128233
});
129234
}
130235

0 commit comments

Comments
 (0)