|
1 |
| - |
2 | 1 | const fs = require('fs')
|
3 | 2 | const fetch = require('node-fetch')
|
| 3 | + |
4 | 4 | const {
|
5 |
| - _structuredDataTest, |
6 | 5 | structuredDataTest,
|
7 |
| - structuredDataTestUrl, |
8 |
| - structuredDataTestHtml |
| 6 | + structuredDataTestUrl |
9 | 7 | } = require('../index')
|
10 | 8 | const presets = require('../presets')
|
11 | 9 |
|
12 |
| - |
13 |
| -// @FIXME Split out this single catch-all example file to test multiple scenarios separately |
14 | 10 | const testFile = '__tests__/fixtures/example.html'
|
15 | 11 | const html = fs.readFileSync(testFile)
|
16 | 12 |
|
| 13 | +// Used to hold the test result that is used across these tests |
17 | 14 | let structuredDataTestResult = null
|
18 | 15 |
|
19 |
| -describe('Structured Data parsing', () => { |
| 16 | +describe('Parsing and options', () => { |
20 | 17 | beforeAll(async () => {
|
21 | 18 | // Mock fetch request
|
22 | 19 | fetch.resetMocks()
|
23 | 20 | fetch.mockResponse(html);
|
24 | 21 |
|
25 |
| - // Current test uses single example file |
| 22 | + // @TODO This test uses a single fixture for all tests |
26 | 23 | // Ideally there would be multiple different fixtures that more robustly
|
27 |
| - // test different scenarios, but this is is a practical approach that |
28 |
| - // improves coverage easily for now with minimal effort |
| 24 | + // test different scenarios, but this is is a practical approach that gives |
| 25 | + // good coverage with minimal effort. |
29 | 26 | await structuredDataTest(html, { presets: [ presets.Google, presets.Twitter ]})
|
30 |
| - .then(response => { |
31 |
| - structuredDataTestResult = response |
32 |
| - }) |
33 |
| - .catch(err => { |
34 |
| - structuredDataTestResult = err.res |
35 |
| - }) |
| 27 | + .then(response => { structuredDataTestResult = response }) |
| 28 | + .catch(err => { structuredDataTestResult = err.res }) |
36 | 29 | })
|
37 | 30 |
|
38 | 31 | afterAll(() => {
|
39 | 32 | fetch.resetMocks()
|
40 | 33 | })
|
41 | 34 |
|
42 |
| - test('should auto-detect and return structured data schemas found', async () => { |
43 |
| - expect(structuredDataTestResult.schemas.length).toEqual(4) |
44 |
| - expect(structuredDataTestResult.failed.length).toEqual(0) |
45 |
| - }) |
46 |
| - |
47 |
| - test('should auto-detect when input is HTML and validate all schemas found', async () => { |
48 |
| - expect(structuredDataTestResult.passed.length).toBeGreaterThan(10) |
49 |
| - expect(structuredDataTestResult.failed.length).toEqual(0) |
50 |
| - }) |
51 |
| - |
52 |
| - test('should auto-detect when input is a string', async () => { |
53 |
| - const result = await structuredDataTest(html.toString(), { presets: [ presets.Google ]}) |
54 |
| - expect(result.passed.length).toBeGreaterThan(10) |
55 |
| - expect(result.failed.length).toEqual(0) |
56 |
| - }) |
57 |
| - |
58 |
| - test('should auto-detect when input is a buffer', async () => { |
59 |
| - const result = await new Promise((resolve) => { |
60 |
| - fs.readFile(testFile, async (err, buffer) => { |
61 |
| - return resolve(await structuredDataTest(buffer, { presets: [ presets.Google ]})) |
62 |
| - }) |
63 |
| - }) |
64 |
| - expect(result.passed.length).toBeGreaterThan(10) |
65 |
| - expect(result.failed.length).toEqual(0) |
66 |
| - }) |
67 |
| - |
68 |
| - test('should auto-detect when input is a readable stream', async () => { |
69 |
| - const buffer = fs.createReadStream(testFile) |
70 |
| - const result = await structuredDataTest(buffer, { presets: [ presets.Google ]}) |
71 |
| - expect(result.passed.length).toBeGreaterThan(10) |
72 |
| - expect(result.failed.length).toEqual(0) |
73 |
| - }) |
74 |
| - |
75 |
| - test('should auto-detect when input is an HTTP URL', async () => { |
76 |
| - const result = await structuredDataTest('http://example.com', { presets: [ presets.Google ]}) |
77 |
| - expect(result.passed.length).toBeGreaterThan(10) |
78 |
| - expect(result.failed.length).toEqual(0) |
79 |
| - }) |
80 |
| - |
81 |
| - test('should auto-detect when input is an HTTPS URL', async () => { |
82 |
| - const result = await structuredDataTest('https://example.com', { presets: [ presets.Google ]}) |
83 |
| - expect(result.passed.length).toBeGreaterThan(10) |
84 |
| - expect(result.failed.length).toEqual(0) |
85 |
| - }) |
86 |
| - |
87 |
| - test('should work when explicitly invoked with HTML', async () => { |
88 |
| - const result = await structuredDataTestHtml(html, { presets: [ presets.Google ]}) |
89 |
| - expect(result.passed.length).toBeGreaterThan(10) |
90 |
| - expect(result.failed.length).toEqual(0) |
91 |
| - }) |
92 |
| - |
93 |
| - test('should work when explicitly invoked with a URL', async () => { |
94 |
| - const result = await structuredDataTestUrl('https://example.com', { presets: [ presets.Google ]}) |
95 |
| - expect(result.passed.length).toBeGreaterThan(10) |
96 |
| - expect(result.failed.length).toEqual(0) |
97 |
| - }) |
98 |
| - |
99 | 35 | // @FIXME This test covers too much at once, should split out error handling checks
|
100 | 36 | // @FIXME This test also tests ever preset for coverage - that should move to the presets test file
|
101 | 37 | test('should validate all structured data schemas found as well as any presets specified and handle errors correctly', async () => {
|
|
0 commit comments