ResourceLinks.test.js
6.11 KB
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
'use strict';
const expect = require('chai').expect
, v3Api = require('../v3').api
, discovery = require('../v3').discovery
, testValues = require('../../test/support/testValues.js')
, ResourceLink = require('@peter-murray/hue-bridge-model').model.ResourceLink
;
describe('Hue API #resourceLinks', () => {
let hue;
before(() => {
return discovery.nupnpSearch()
.then(searchResults => {
const localApi = v3Api.createLocal(searchResults[0].ipaddress);
return localApi.connect(testValues.username)
.then(api => {
hue = api;
});
});
});
describe('#getAll()', () => {
it('should get all resource links', async () => {
const resourceLinks = await hue.resourceLinks.getAll();
expect(resourceLinks).to.be.instanceOf(Array);
const resourceLink = resourceLinks[0];
// console.log(resourceLink.toStringDetailed());
expect(resourceLink instanceof ResourceLink).to.be.true;
});
});
describe('#get()', () => {
it('should get a resource link that exists using an id', async () => {
const allResourceLinks = await hue.resourceLinks.getAll()
, targetResourceLink = allResourceLinks[allResourceLinks.length - 1]
;
const resourceLink = await hue.resourceLinks.getResourceLink(targetResourceLink.id);
expect(resourceLink instanceof ResourceLink).to.be.true;
expect(resourceLink).to.have.property('id').to.equal(targetResourceLink.id);
//TODO need to do a deep equals on contents against targetResourceLink
expect(resourceLink).to.have.property('name').to.equal(targetResourceLink.name);
});
it('should get a resource link that exists using a ResourceLink', async () => {
const allResourceLinks = await hue.resourceLinks.getAll()
, targetResourceLink = allResourceLinks[allResourceLinks.length - 1]
;
const resourceLink = await hue.resourceLinks.getResourceLink(targetResourceLink);
expect(resourceLink instanceof ResourceLink).to.be.true;
expect(resourceLink).to.have.property('id').to.equal(targetResourceLink.id);
//TODO need to do a deep equals on contents against targetResourceLink
expect(resourceLink).to.have.property('name').to.equal(targetResourceLink.name);
});
//TODO test get failure
});
describe('#createResourceLink()', () => {
let createdResourceLinkId;
beforeEach(() => {
createdResourceLinkId = null;
});
afterEach(async () => {
if (createdResourceLinkId) {
await hue.resourceLinks.deleteResourceLink(createdResourceLinkId);
}
});
it('should create a resource link', async () => {
const resourceLink = new ResourceLink();
resourceLink.name = 'Test ResourceLink';
resourceLink.description = 'A test resource link for node-hue-api';
resourceLink.recycle = true;
resourceLink.classid = 100;
resourceLink.addLink('groups', 0);
const result = await hue.resourceLinks.createResourceLink(resourceLink);
expect(result instanceof ResourceLink).to.be.true;
createdResourceLinkId = result.id;
expect(result).to.have.property('name').to.equal(resourceLink.name);
expect(result).to.have.property('description').to.equal(resourceLink.description);
expect(result).to.have.property('recycle').to.equal(resourceLink.recycle);
expect(result).to.have.property('classid').to.equal(resourceLink.classid);
expect(result).to.have.property('links').to.have.property('groups').to.have.members(['0']);
// Owner should be set on resultant ResourceLinks
expect(result).to.have.property('owner').to.equal(testValues.username);
});
it('should fail to create on an incomplete resource link', async () => {
const resourceLink = new ResourceLink();
resourceLink.name = 'Test Resource Link';
resourceLink.description = 'A test resource link for node-hue-api';
resourceLink.recycle = true;
try {
await hue.resourceLinks.createResourceLink(resourceLink);
expect.fail('Should have thrown exception above');
} catch (err) {
expect(err.message).to.contain('some links defined');
}
});
});
describe('#deleteResourceLink()', () => {
it('should delete an existing resource link', async () => {
const resourceLink = new ResourceLink();
resourceLink.name = 'Test ResourceLink to be deleted';
resourceLink.description = 'A test resource link for node-hue-api';
resourceLink.recycle = true;
resourceLink.classid = 1;
resourceLink.addLink('groups', 0);
const createdResourceLink = await hue.resourceLinks.createResourceLink(resourceLink);
expect(createdResourceLink).to.have.property('id');
const result = await hue.resourceLinks.deleteResourceLink(createdResourceLink.id);
expect(result).to.be.true;
});
});
describe('#updateResourceLink()', () => {
let existingResourceLink;
beforeEach(async () => {
const resourceLink = new ResourceLink();
resourceLink.name = 'Update Resource Link Tests';
resourceLink.description = 'A test resource link for node-hue-api';
resourceLink.recycle = true;
resourceLink.classid = 1;
resourceLink.addLink('groups', 0);
existingResourceLink = await hue.resourceLinks.createResourceLink(resourceLink);
});
afterEach(async () => {
if (existingResourceLink) {
await hue.resourceLinks.deleteResourceLink(existingResourceLink);
}
});
it('should update the name of a resource link', async () => {
const newName = `RL ${Date.now()}`;
existingResourceLink.name = newName;
const updated = await hue.resourceLinks.updateResourceLink(existingResourceLink);
expect(updated).to.have.property('name').to.be.true;
expect(updated).to.have.property('description').to.be.true;
expect(updated).to.have.property('classid').to.be.true;
expect(updated).to.have.property('links').to.be.true;
const resourceLink = await hue.resourceLinks.getResourceLink(existingResourceLink.id);
expect(resourceLink).to.have.property('name').to.equal(newName);
});
});
});