forked from electron/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors-spec.js
179 lines (150 loc) · 5.43 KB
/
colors-spec.js
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
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict'
const fs = require('fs')
const os = require('os')
const path = require('path')
const sinon = require('sinon')
const chai = require('chai')
const Jimp = require('jimp')
const tinyColor = require('tinycolor2')
chai.should()
const expect = chai.expect
const Colors = require('../lib/colors.js')
describe('colors', function () {
let consoleInfo
let consoleError
let testDir
let slugsAndIconPaths
const colors = ['white', 'black']
beforeEach(async function () {
slugsAndIconPaths = []
// create a couple of test icons in a tmpdir
testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'colors-spec-'))
for (const colorName of colors) {
const c = parseInt(tinyColor(colorName).toHex8(), 16)
const image = new Jimp(2, 2, c)
const iconPath = path.join(testDir, colorName + '.png')
await new Promise((resolve, reject) =>
image.write(iconPath, (err, buffer) =>
err ? reject(err) : resolve(buffer)
)
)
fs.chmodSync(iconPath, 511)
slugsAndIconPaths.push({ slug: colorName, iconPath })
}
consoleError = sinon.stub(console, 'error')
consoleInfo = sinon.stub(console, 'info')
})
afterEach(() => {
// remove the temporaries that were created in before()
for (const entry of fs.readdirSync(testDir)) {
fs.unlinkSync(path.resolve(testDir, entry))
}
fs.rmdirSync(testDir)
consoleError.restore()
consoleInfo.restore()
})
it('should create entries with the expected properties', async () => {
// test input
const entry = slugsAndIconPaths[0]
const colors = await Colors.getColors([entry], {}, testDir)
colors.should.have
.keys(entry.slug)
.and.property(entry.slug)
.has.all.keys(
'source',
'faintColorOnWhite',
'goodColorOnBlack',
'goodColorOnWhite',
'palette'
)
.and.property('source')
.has.all.keys('path', 'revHash')
.and.property('path')
.equals(path.basename(entry.iconPath))
}).timeout(5000)
it('should add an entry when a new app is added', async () => {
const oldColors = await Colors.getColors(
slugsAndIconPaths.slice(0, 1),
{},
testDir
)
const newColors = await Colors.getColors(
slugsAndIconPaths.slice(0, 2),
oldColors,
testDir
)
// newColors should be a superset of oldColors
newColors.should.deep.contain(oldColors)
oldColors.should.not.deep.contain(newColors)
expect(consoleInfo.callCount).to.equal(2)
})
it('should remove an entry when an app is removed', async () => {
const oldColors = await Colors.getColors(
slugsAndIconPaths.slice(0, 2),
{},
testDir
)
const newColors = await Colors.getColors(
slugsAndIconPaths.slice(0, 1),
oldColors,
testDir
)
// newColors should be a subset of oldColors
newColors.should.not.deep.contain(oldColors)
oldColors.should.deep.contain(newColors)
expect(consoleInfo.callCount).to.equal(2)
})
it('should create reproducible output', async () => {
const a = await Colors.getColors(slugsAndIconPaths, {}, testDir)
const b = await Colors.getColors(slugsAndIconPaths, {}, testDir)
a.should.deep.equal(b)
expect(consoleInfo.callCount).to.equal(4)
})
it('should skip entries whose icons are unreadable', async () => {
const badEntry = slugsAndIconPaths[0]
const goodEntry = slugsAndIconPaths[1]
const input = [badEntry, goodEntry]
// make the first icon unreadable
fs.unlinkSync(badEntry.iconPath)
const colors = await Colors.getColors(input, {}, testDir)
colors.should.have.keys(goodEntry.slug).and.not.have.keys(badEntry.slug)
expect(consoleError.callCount).to.equal(1)
expect(consoleError.firstCall.args[0]).to.include(badEntry.iconPath)
expect(consoleInfo.callCount).to.equal(1)
})
it('should skip entries whose icons are unparsable', async () => {
const entries = slugsAndIconPaths.map((original) => Object.create(original))
const badEntry = entries[0]
const goodEntry = entries[1]
badEntry.iconPath = path.join(testDir, 'hello.png')
fs.writeFileSync(
badEntry.iconPath,
'This is a text file! The file suffix is wrong!\n'
)
const colors = await Colors.getColors(entries, {}, testDir)
colors.should.have.keys(goodEntry.slug).and.not.have.keys(badEntry.slug)
expect(consoleError.callCount).to.equal(1)
expect(consoleError.firstCall.args[0]).to.include(badEntry.iconPath)
expect(consoleInfo.callCount).to.equal(2)
})
it('should update revHashes when icon files change', async () => {
let entries = slugsAndIconPaths.map((original) => Object.create(original))
const oldColors = await Colors.getColors(entries, {}, testDir)
entries = slugsAndIconPaths.map((original) => Object.create(original))
const changedEntry = entries[0]
const unchangedEntry = entries[1]
changedEntry.iconPath = unchangedEntry.iconPath
const newColors = await Colors.getColors(entries, oldColors, testDir)
// the revHash on the unchanged entry should be unchanged
expect(newColors)
.property(unchangedEntry.slug)
.to.deep.contain(oldColors[unchangedEntry.slug])
// the revHash on the changed entry should be different
expect(newColors)
.property(changedEntry.slug)
.property('source')
.property('revHash')
.should.not.equal(oldColors[changedEntry.slug].source.revHash)
expect(consoleInfo.callCount).to.equal(3)
})
})