-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyeelight.test.js
89 lines (74 loc) · 2.9 KB
/
yeelight.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
86
87
88
89
'use strict';
const chai = require('chai')
, expect = chai.expect
, sinon = require('sinon');
const Yeelight = require('../');
describe('_constrain', () => {
it('should limit a number to the max value', () => {
const yee = new Yeelight();
expect(yee._constrain(100, 10, 25)).to.equal(25);
});
it('should limit a number to the min value', () => {
const yee = new Yeelight();
expect(yee._constrain(1, 10, 25)).to.equal(10);
});
it('should not affect valid values', () => {
const yee = new Yeelight();
expect(yee._constrain(5, 0, 10)).to.equal(5);
});
it('should set non numbers to the minimum', () => {
const yee = new Yeelight();
expect(yee._constrain(NaN, 0, 10)).to.equal(0);
expect(yee._constrain([], 0, 10)).to.equal(0);
expect(yee._constrain('string', 0, 10)).to.equal(0);
});
});
describe('_inArray', () => {
it('should find an item in an array', () => {
const yee = new Yeelight();
expect(yee._inArray('findMe', ['foo', 2, 'bar', 'findMe', 'baz'])).to.equal(true);
});
it('should not find an item which is not in the array', () => {
const yee = new Yeelight();
expect(yee._inArray('findMeNot', ['foo', 2, 'bar', 'findMe', 'baz'])).to.equal(false);
});
it('should return false when given a non array as the haystack', () => {
const yee = new Yeelight();
expect(yee._inArray('string', 'notAnArray')).to.equal(false);
});
});
describe('_ifValid', () => {
it('should return the input if valid', () => {
const yee = new Yeelight();
expect(yee._ifValid('validString', ['validString', 2, 3, 4], 'default')).to.equal('validString');
expect(yee._ifValid('validString', ['validString', 2, 3, 4])).to.equal('validString');
});
it('should return the default if input is invalid', () => {
const yee = new Yeelight();
expect(yee._ifValid('invalidString', ['validString', 2, 3, 4], 'default')).to.equal('default');
expect(yee._ifValid('invalidString', ['validString', 2, 3, 4])).to.equal('');
});
});
describe('Yeelight Class', () => {
const yee = new Yeelight({ip: '0.0.0.0', port: 55443});
this.write = sinon.stub(yee.client, 'write').returns(true);
this.connect = sinon.stub(yee.client, 'connect');
it('should emit values coming from the data listener', (done) => {
yee.on('data', (data) => {
expect(data).to.equal(true);
done();
});
yee.emit('data', true);
});
it('should emit values coming from the close listener', (done) => {
yee.on('close', (data) => {
expect(yee.connected).to.equal(false);
expect(data).to.be.an('object');
done();
});
yee.emit('close', {});
});
it('should touch the command sneder with toggle', () => {
yee.toggle();
});
});