Selaa lähdekoodia

:recycle: test channels

master
j 2 vuotta sitten
vanhempi
commit
a51c11384c
3 muutettua tiedostoa jossa 44 lisäystä ja 15 poistoa
  1. 3
    0
      src/channel.js
  2. 36
    0
      test/channel.spec.js
  3. 5
    15
      test/system.spec.js

+ 3
- 0
src/channel.js Näytä tiedosto

@@ -29,6 +29,9 @@ class Channel {
29 29
     get interval() {
30 30
         return this._interval
31 31
     }
32
+    get lastUpdate() {
33
+        return this._reader.created
34
+    }
32 35
     get val() {
33 36
         return this._reader.val
34 37
     }

+ 36
- 0
test/channel.spec.js Näytä tiedosto

@@ -0,0 +1,36 @@
1
+import { expect, test } from 'vitest'
2
+import { Channel } from '../src/channel.js'
3
+import { Reading } from '../src/channel.js'
4
+
5
+test('channel - instantiate channel with readers correctly', () => {
6
+    let now = Date.now()
7
+    let mockTemp = 70
8
+    let testReader = new Reading({
9
+        onRead: () => mockTemp,
10
+        unit: 'F',
11
+        max: 100,
12
+        min: 50,
13
+    })
14
+    let testChan = new Channel({ interval: 1, reader: testReader })
15
+
16
+    // Test that the channel and channel.reader are working
17
+    expect(testChan.lastUpdate == now).toStrictEqual(true)
18
+    expect(testChan.val).toStrictEqual(70)
19
+    expect(testChan.unit).toStrictEqual('F')
20
+
21
+    mockTemp = 101
22
+    testChan = testChan.update()
23
+    expect(testChan.lastUpdate > now).toStrictEqual(true)
24
+    expect(testChan.val).toStrictEqual(101)
25
+    expect(testChan.inRange).toStrictEqual(false)
26
+    expect(testChan.aboveRange).toStrictEqual(true)
27
+    expect(testChan.belowRange).toStrictEqual(false)
28
+
29
+    mockTemp = 49
30
+    testChan = testChan.update()
31
+    now = Date.now()
32
+    expect(testChan.lastUpdate == now).toStrictEqual(true)
33
+    expect(testChan.inRange).toStrictEqual(false)
34
+    expect(testChan.aboveRange).toStrictEqual(false)
35
+    expect(testChan.belowRange).toStrictEqual(true)
36
+})

+ 5
- 15
test/system.spec.js Näytä tiedosto

@@ -1,6 +1,5 @@
1 1
 import { expect, test } from 'vitest'
2 2
 import { System, makeContainerT } from '../src/system.js'
3
-import { Reading } from '../src/channel.js'
4 3
 import { Container } from '../src/container.js'
5 4
 import { controllerTypes } from '../src/controller.js'
6 5
 import { InputConf } from '../src/index.js'
@@ -18,11 +17,14 @@ test('system - instantiates and stores inventory correctly', () => {
18 17
     const outputs = testSystem.outputsFor({ id: 'aaa' })
19 18
     expect(outputs).toStrictEqual(['output_test'])
20 19
 
20
+    testSystem.replaceContainer({ id: 'aaa' })
21
+    expect(testSystem.containers[0].id).toStrictEqual('aaa')
22
+
21 23
     testSystem.remove('aaa')
22 24
     expect(testSystem.inventory).toStrictEqual({})
23 25
 })
24 26
 
25
-test('system - instiantes channels with readers correctly', () => {
27
+test('system - update channels with readers correctly', () => {
26 28
     let testSystem = new System()
27 29
     let testContainer = new Container({ l: 10, w: 10, h: 10 })
28 30
     let mockTemp = 70
@@ -30,10 +32,7 @@ test('system - instiantes channels with readers correctly', () => {
30 32
         controllerTypes.temp,
31 33
         () => mockTemp,
32 34
         0.1,
33
-        () => {
34
-            console.log('tick')
35
-            return 'tick'
36
-        }
35
+        () => 'tick'
37 36
     )
38 37
     const cT = makeContainerT([testInConf], [], testContainer)
39 38
     testSystem.add('aaa', cT)
@@ -42,14 +41,5 @@ test('system - instiantes channels with readers correctly', () => {
42 41
 
43 42
     let chan = testSystem.inputs[0].channel
44 43
     expect(chan.interval).toStrictEqual(0.1)
45
-
46
-    // Test that the channel and channel.reader are working
47
-    expect(chan.val).toStrictEqual(70)
48
-    expect(chan.unit).toStrictEqual('F')
49
-    expect(chan.inRange).toStrictEqual(false)
50 44
     expect(testSystem.inputs[0].onUpdate()).toStrictEqual('tick')
51
-
52
-    mockTemp = 79
53
-    chan = chan.update()
54
-    expect(chan.val).toStrictEqual(79)
55 45
 })

Loading…
Peruuta
Tallenna