|
| 1 | +import Test, { TestParameters, Result } from '../Test'; |
| 2 | +import request from '../request'; |
| 3 | +import logger from '../logger'; |
| 4 | +import { getHeading, parseHtml } from '../functions'; |
| 5 | + |
| 6 | +class Heading extends Test { |
| 7 | + public name = 'Heading'; |
| 8 | + |
| 9 | + public async test({ url }: TestParameters): Promise<Result> { |
| 10 | + logger.info(`Starting ${this.constructor.name} test...`); |
| 11 | + |
| 12 | + const response = await request.get(url); |
| 13 | + const html = await parseHtml(response); |
| 14 | + const heading = getHeading(html); |
| 15 | + const subTests = this.checkHeading(heading); |
| 16 | + |
| 17 | + return { |
| 18 | + status: this.getStatus(subTests.map(test => test.status)), |
| 19 | + title: this.constructor.name, |
| 20 | + description: '', |
| 21 | + results: subTests, |
| 22 | + }; |
| 23 | + } |
| 24 | + |
| 25 | + private checkHeading(title: string | string[]): Array<Result> { |
| 26 | + const results = []; |
| 27 | + |
| 28 | + results.push({ |
| 29 | + status: typeof title !== undefined && title.length > 0 ? 'SUCCESS' : 'WARNING', |
| 30 | + title: 'H1 tag', |
| 31 | + }); |
| 32 | + |
| 33 | + results.push({ |
| 34 | + status: Array.isArray(title) ? 'ERROR' : 'SUCCESS', |
| 35 | + title: 'Duplicate H1 tag', |
| 36 | + description: `HTML should contain just one title tag.`, |
| 37 | + }); |
| 38 | + |
| 39 | + results.push({ |
| 40 | + status: title.length <= 60 ? 'SUCCESS' : 'WARNING', |
| 41 | + title: 'Title length', |
| 42 | + description: `Title length should be under 60 characters and it is ${title.length}.`, |
| 43 | + }); |
| 44 | + |
| 45 | + return results; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +export default Heading; |
0 commit comments