-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.spec.js
227 lines (187 loc) · 8.22 KB
/
module.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const { expect } = require('chai')
const fs = require('fs')
const path = require('path')
const {
getAllDisplays,
getPrimaryDisplay,
getDisplayFromID,
mirror,
screenshot,
} = require('../index')
describe('node-mac-displays', () => {
describe('getAllDisplays', () => {
it('returns an array of Display objects', () => {
const displays = getAllDisplays()
expect(displays).to.be.an('array')
const display = displays[0]
expect(display).to.have.property('id').that.is.a('number')
expect(display).to.have.property('name').that.is.a('string')
expect(display).to.have.property('supportedWindowDepths').that.is.an('array')
expect(display).to.have.property('rotation').that.is.a('number')
expect(display).to.have.property('scaleFactor').that.is.a('number')
expect(display).to.have.property('isMonochrome').that.is.a('boolean')
expect(display).to.have.property('depth').that.is.a('number')
expect(display).to.have.property('internal').that.is.a('boolean')
expect(display).to.have.property('isAsleep').that.is.a('boolean')
expect(display).to.have.property('refreshRate').that.is.a('number')
expect(display).to.have.property('colorSpace').that.is.an('object')
const { name, componentCount } = display.colorSpace
expect(name).to.be.a('string')
expect(componentCount).to.be.a('number')
expect(display).to.have.property('bounds').that.is.an('object')
const bounds = display.bounds
expect(bounds.x).to.be.a('number')
expect(bounds.y).to.be.a('number')
expect(bounds.width).to.be.a('number')
expect(bounds.height).to.be.a('number')
expect(display).to.have.property('workArea').that.is.an('object')
const workArea = display.bounds
expect(workArea.x).to.be.a('number')
expect(workArea.y).to.be.a('number')
expect(workArea.width).to.be.a('number')
expect(workArea.height).to.be.a('number')
})
})
describe('getPrimaryDisplay', () => {
it('returns the primary display', () => {
const display = getPrimaryDisplay()
expect(display).to.have.property('id').that.is.a('number')
expect(display).to.have.property('name').that.is.a('string')
expect(display).to.have.property('supportedWindowDepths').that.is.an('array')
expect(display).to.have.property('rotation').that.is.a('number')
expect(display).to.have.property('scaleFactor').that.is.a('number')
expect(display).to.have.property('isMonochrome').that.is.a('boolean')
expect(display).to.have.property('depth').that.is.a('number')
expect(display).to.have.property('internal').that.is.a('boolean')
expect(display).to.have.property('colorSpace').that.is.an('object')
const { name, componentCount } = display.colorSpace
expect(name).to.be.a('string')
expect(componentCount).to.be.a('number')
expect(display).to.have.property('bounds').that.is.an('object')
const bounds = display.bounds
expect(bounds.x).to.be.a('number')
expect(bounds.y).to.be.a('number')
expect(bounds.width).to.be.a('number')
expect(bounds.height).to.be.a('number')
expect(display).to.have.property('workArea').that.is.an('object')
const workArea = display.bounds
expect(workArea.x).to.be.a('number')
expect(workArea.y).to.be.a('number')
expect(workArea.width).to.be.a('number')
expect(workArea.height).to.be.a('number')
})
})
describe('getDisplayFromID', () => {
it('throws an error if id is not a number', () => {
expect(() => {
getDisplayFromID('im a string!!')
}).to.throw(`'id' must be a number`)
})
it('returns a valid display given an ID', () => {
const { id } = getPrimaryDisplay()
const display = getDisplayFromID(id)
expect(display).to.have.property('id').that.is.a('number')
expect(display).to.have.property('name').that.is.a('string')
expect(display).to.have.property('supportedWindowDepths').that.is.an('array')
expect(display).to.have.property('rotation').that.is.a('number')
expect(display).to.have.property('scaleFactor').that.is.a('number')
expect(display).to.have.property('isMonochrome').that.is.a('boolean')
expect(display).to.have.property('depth').that.is.a('number')
expect(display).to.have.property('internal').that.is.a('boolean')
expect(display).to.have.property('colorSpace').that.is.an('object')
const { name, componentCount } = display.colorSpace
expect(name).to.be.a('string')
expect(componentCount).to.be.a('number')
expect(display).to.have.property('bounds').that.is.an('object')
const bounds = display.bounds
expect(bounds.x).to.be.a('number')
expect(bounds.y).to.be.a('number')
expect(bounds.width).to.be.a('number')
expect(bounds.height).to.be.a('number')
expect(display).to.have.property('workArea').that.is.an('object')
const workArea = display.bounds
expect(workArea.x).to.be.a('number')
expect(workArea.y).to.be.a('number')
expect(workArea.width).to.be.a('number')
expect(workArea.height).to.be.a('number')
})
})
describe('mirror', () => {
it('throws an error if enable is not a boolean', () => {
expect(() => {
mirror('im a string!!')
}).to.throw(`'enable' must be a boolean`)
})
it('throws an error if firstID is not valid', () => {
expect(() => {
mirror(true, 'bad-type')
}).to.throw(`'firstID' must be a number`)
})
it('throws an error if options.type is not valid', () => {
expect(() => {
const { id } = getPrimaryDisplay()
mirror(true, id, 'bad-type')
}).to.throw(`'secondID' must be a number`)
})
})
describe('screenshot', () => {
it('throws an error if id is not a number', () => {
expect(() => {
screenshot('im a string!!')
}).to.throw(`'id' must be a number`)
})
it('throws an error if options.type is not valid', () => {
expect(() => {
const { id } = getPrimaryDisplay()
screenshot(id, { type: 'bad-type' })
}).to.throw(`'type' must be one of jpeg, tiff, png`)
})
it('throws an error if options.bounds are not valid', () => {
const { id } = getPrimaryDisplay()
expect(() => {
screenshot(id, { bounds: 'oh no' })
}).to.throw(`'bounds' must be an object`)
expect(() => {
screenshot(id, { bounds: { x: 'bad', y: 1, width: 10, height: 10 } })
}).to.throw(`'bounds.x' must be a number`)
expect(() => {
screenshot(id, { bounds: { x: 1, y: 'bad', width: 10, height: 10 } })
}).to.throw(`'bounds.y' must be a number`)
expect(() => {
screenshot(id, { bounds: { x: 1, y: 1, width: 'bad', height: 10 } })
}).to.throw(`'bounds.width' must be a number`)
expect(() => {
screenshot(id, { bounds: { x: 1, y: 1, width: 10, height: 'bad' } })
}).to.throw(`'bounds.height' must be a number`)
})
it('throws an error if one of options.bounds is undefined', () => {
const { id } = getPrimaryDisplay()
expect(() => {
screenshot(id, { bounds: { y: 1, width: 10, height: 10 } })
}).to.throw(`'bounds.x' must be a number`)
expect(() => {
screenshot(id, { bounds: { x: 1, width: 10, height: 10 } })
}).to.throw(`'bounds.y' must be a number`)
expect(() => {
screenshot(id, { bounds: { x: 1, y: 1, height: 10 } })
}).to.throw(`'bounds.width' must be a number`)
expect(() => {
screenshot(id, { bounds: { x: 1, y: 1, width: 10 } })
}).to.throw(`'bounds.height' must be a number`)
})
it('can take screenshot if options.bounds are 0', () => {
const { id } = getPrimaryDisplay()
const screenshotData = screenshot(id, { bounds: { x: 0, y: 0, width: 0, height: 0 } })
expect(screenshotData).to.be.instanceof(Buffer)
})
it('can write out a screenshot to the current directory', () => {
const { id } = getPrimaryDisplay()
const ssPath = path.resolve(__dirname, 'screenshot.jpg')
const screenshotData = screenshot(id)
fs.writeFileSync(ssPath, screenshotData)
expect(fs.existsSync(ssPath)).to.be.true
fs.unlinkSync(ssPath)
expect(fs.existsSync(ssPath)).to.be.false
})
})
})