-
Notifications
You must be signed in to change notification settings - Fork 1
/
doorlock.test.js
85 lines (76 loc) · 2.54 KB
/
doorlock.test.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
const DoorLock = require('./doorlock')
const tessel = require('tessel')
const fs = require('fs')
const { CARDS_PATH, RFID_READER_SERIAL_NUMBER } = require('./constants')
jest.mock('tessel')
jest.mock('fs')
describe('models/doorlock', () => {
const filePath = '/path/to/cards.json'
let doorLock
beforeEach(() => {
doorLock = new DoorLock()
})
describe('.findRFIDReaderDevice', () => {
test('should find a device by its serial number', () => {
const device = { serialNumber: RFID_READER_SERIAL_NUMBER }
const devices = [device, { serialNumber: 'foo' }]
const found = doorLock.findRFIDReaderDevice(devices)
expect(device).toBe(found)
})
})
describe('.validateCard', () => {
test('should match existing card', () => {
const expected = 5109933
const scanned = '3F004DF8AD27'
const cards = [{ name: 'John', number: `0000${expected}}` }]
jest
.spyOn(doorLock, 'readCardsFromSDCard')
.mockImplementationOnce(() => Promise.resolve(cards))
jest
.spyOn(doorLock, 'openDoor')
.mockImplementationOnce(() => Promise.resolve())
return doorLock
.validateCard(scanned)
.then(() => expect(doorLock.openDoor).toBeCalled())
})
})
describe('.openDoor', () => {
test('Should turn on success LED', () => {
jest.spyOn(doorLock, 'closeDoor')
return doorLock.openDoor().then(() => {
expect(tessel.led[3].on).toBeCalled()
expect(doorLock.closeDoor).toBeCalled()
})
})
})
describe('.closeDoor', () => {
test('should turn off success LED', () => {
doorLock.closeDoor()
expect(tessel.led[3].off).toBeCalled()
})
})
describe('.readCardsFromSDCard', () => {
test('should read and convert file data to JSON', () => {
const cards = [{ name: 'John', number: '123' }]
jest
.spyOn(fs, 'readFile')
.mockImplementationOnce((p, cb) => cb(null, JSON.stringify(cards)))
return doorLock.readCardsFromSDCard(filePath).then(actual => {
expect(actual).toEqual(cards)
})
})
})
describe('.writeCardsToSDCard', () => {
xtest('should write data to SD card', () => {
const cards = [{ name: 'John', number: '123' }]
jest.spyOn(fs, 'writeFile').mockImplementationOnce((path, text, cb) => {
console.log('TEXT', text)
console.log('PATH', path)
expect(path).toBe(CARDS_PATH)
expect(text).toBe(JSON.stringify(cards))
cb()
})
return expect(doorLock.writeCardsToSDCard(cards)).resolves.toBe()
})
})
})