forked from WebThingsIO/webthing-node-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththing.test.js
More file actions
71 lines (64 loc) · 2.2 KB
/
thing.test.js
File metadata and controls
71 lines (64 loc) · 2.2 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
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import Thing from '../src/thing.js';
describe('Thing', () => {
const partialTD = {
title: 'Test Thing',
description: 'A thing for testing',
properties: {
on: {
type: 'boolean',
title: 'On/Off',
},
},
};
describe('constructor', () => {
it('should parse and populate mandatory members of the Thing', () => {
const thing = new Thing(partialTD);
assert.equal(thing.context, 'https://www.w3.org/2022/wot/td/v1.1');
assert.equal(thing.title, partialTD.title);
assert.deepEqual(thing.securityDefinitions, {
nosec_sc: { scheme: 'nosec' },
});
assert.equal(thing.security, 'nosec_sc');
});
});
describe('getThingDescription', () => {
it('should return the Thing Description', () => {
const thing = new Thing(partialTD);
const td = thing.getThingDescription();
assert.deepEqual(td, {
'@context': 'https://www.w3.org/2022/wot/td/v1.1',
title: 'Test Thing',
securityDefinitions: { nosec_sc: { scheme: 'nosec' } },
security: 'nosec_sc',
});
});
});
describe('setPropertyReadHandler', () => {
it('should register a property read handler', () => {
const thing = new Thing(partialTD);
const handler = () => true;
thing.setPropertyReadHandler('on', handler);
assert.strictEqual(thing.propertyReadHandlers.has('on'), true);
});
});
describe('readProperty', () => {
it('should return the value from the property read handler', () => {
const thing = new Thing(partialTD);
thing.setPropertyReadHandler('on', () => true);
const value = thing.readProperty('on');
assert.strictEqual(value, true);
});
it('should support async property read handlers', async () => {
const thing = new Thing(partialTD);
thing.setPropertyReadHandler('on', async () => false);
const value = await thing.readProperty('on');
assert.strictEqual(value, false);
});
it('should throw when no handler is registered', () => {
const thing = new Thing(partialTD);
assert.throws(() => thing.readProperty('on'));
});
});
});