UNPKG

2.11 kBJavaScriptView Raw
1'use strict'
2
3const fs = require('fs')
4const test = require('tap').test
5const cheerio = require('cheerio')
6const endpoint = require('endpoint')
7const RenderRecommendations = require('../recommendations/index.js')
8
9test('Recommendation - contains all categories', function (t) {
10 new RenderRecommendations().pipe(endpoint(function (err, content) {
11 if (err) return t.ifError(err)
12
13 const doc = cheerio.load(content.toString())
14 const templates = doc('template').map(function (index, rawElement) {
15 const element = doc(rawElement)
16 return {
17 attr: {
18 class: element.attr('class')
19 },
20 data: {
21 issue: element.data('issue'),
22 type: element.data('type'),
23 category: element.data('category'),
24 menu: element.data('menu'),
25 title: element.data('title'),
26 order: element.data('order')
27 }
28 }
29 }).get()
30
31 const categories = new Set()
32 for (const templateElement of templates) {
33 categories.add(templateElement.data.category)
34
35 t.strictEqual(templateElement.attr.class, 'recommendation-text')
36 t.ok(['yes', 'no'].includes(templateElement.data.issue))
37 t.ok(['summary', 'read-more'].includes(templateElement.data.type))
38 t.ok(templateElement.data.menu.length > 0)
39 t.ok(templateElement.data.title.length > 0)
40 t.strictEqual(typeof templateElement.data.order, 'number')
41 }
42
43 t.strictDeepEqual(Array.from(categories).sort(), [
44 'data', 'event-loop', 'gc', 'io', 'none', 'unknown'
45 ])
46 t.end()
47 }))
48})
49
50test('Recommendation - read failure', function (t) {
51 const originalReadFile = fs.readFile
52 t.beforeEach(function (done) {
53 fs.readFile = function (filepath, encoding, callback) {
54 callback(new Error('mocked fs error'))
55 }
56 done()
57 })
58
59 t.afterEach(function (done) {
60 fs.readFile = originalReadFile
61 done()
62 })
63
64 t.test('mock test', function (t) {
65 new RenderRecommendations().pipe(endpoint(function (err, content) {
66 t.strictDeepEqual(err, new Error('mocked fs error'))
67 t.end()
68 }))
69 })
70
71 t.end()
72})