|
| 1 | +import InputCell, ComputeCell from require 'react' |
| 2 | + |
| 3 | +describe 'react', -> |
| 4 | + it 'input cells have a value', -> |
| 5 | + input = InputCell 2 |
| 6 | + assert.are.equal 2, input\get_value! |
| 7 | + |
| 8 | + pending "an input cell's value can be set", -> |
| 9 | + input = InputCell 4 |
| 10 | + input\set_value 20 |
| 11 | + assert.are.equal 20, input\get_value! |
| 12 | + |
| 13 | + pending 'compute cells calculate initial value', -> |
| 14 | + input = InputCell 1 |
| 15 | + output = ComputeCell input, (x) -> x + 1 |
| 16 | + assert.are.equal 2, output\get_value! |
| 17 | + |
| 18 | + pending 'compute cells take inputs in the right order', -> |
| 19 | + one = InputCell 1 |
| 20 | + two = InputCell 2 |
| 21 | + output = ComputeCell one, two, (x, y) -> x + y * 10 |
| 22 | + assert.are.equal 21, output\get_value! |
| 23 | + |
| 24 | + pending 'compute cells update value when dependencies are changed', -> |
| 25 | + input = InputCell 1 |
| 26 | + output = ComputeCell input, (x) -> x + 1 |
| 27 | + |
| 28 | + input\set_value 3 |
| 29 | + assert.are.equal 4, output\get_value! |
| 30 | + |
| 31 | + pending 'compute cells can depend on other compute cells', -> |
| 32 | + input = InputCell 1 |
| 33 | + times_two = ComputeCell input, (x) -> x * 2 |
| 34 | + times_thirty = ComputeCell input, (x) -> x * 30 |
| 35 | + |
| 36 | + output = ComputeCell times_two, times_thirty, (x, y) -> x + y |
| 37 | + assert.are.equal 32, output\get_value! |
| 38 | + |
| 39 | + input\set_value 3 |
| 40 | + assert.are.equal 96, output\get_value! |
| 41 | + |
| 42 | + pending 'compute cells fire callbacks', -> |
| 43 | + input = InputCell 1 |
| 44 | + output = ComputeCell input, (x) -> x + 1 |
| 45 | + |
| 46 | + -- "Spies" are documented here: https://lunarmodules.github.io/busted/#spies-mocks-stubs |
| 47 | + callback = spy.new(->) -- the argument is an empty function |
| 48 | + |
| 49 | + output\watch callback |
| 50 | + input\set_value 3 |
| 51 | + assert.spy(callback).was_called 1 |
| 52 | + assert.spy(callback).was_called_with 4 |
| 53 | + |
| 54 | + pending 'callbacks only fire on change', -> |
| 55 | + input = InputCell 1 |
| 56 | + output = ComputeCell input, (x) -> if x < 3 then 111 else 222 |
| 57 | + callback = spy.new(->) |
| 58 | + |
| 59 | + output\watch callback |
| 60 | + |
| 61 | + input\set_value 2 |
| 62 | + assert.spy(callback).was_called 0 |
| 63 | + |
| 64 | + input\set_value 4 |
| 65 | + assert.spy(callback).was_called 1 |
| 66 | + assert.spy(callback).was_called_with 222 |
| 67 | + |
| 68 | + pending 'callbacks can be added and removed', -> |
| 69 | + input = InputCell 11 |
| 70 | + output = ComputeCell input, (x) -> x + 1 |
| 71 | + callback1 = spy.new(->) |
| 72 | + callback2 = spy.new(->) |
| 73 | + callback3 = spy.new(->) |
| 74 | + |
| 75 | + output\watch callback1 |
| 76 | + output\watch callback2 |
| 77 | + input\set_value 31 |
| 78 | + |
| 79 | + output\unwatch callback1 |
| 80 | + output\watch callback3 |
| 81 | + input\set_value 41 |
| 82 | + |
| 83 | + assert.spy(callback1).was_called 1 |
| 84 | + assert.spy(callback1).was_called_with 32 |
| 85 | + assert.spy(callback2).was_called 2 |
| 86 | + assert.spy(callback2).was_called_with 42 |
| 87 | + assert.spy(callback3).was_called 1 |
| 88 | + assert.spy(callback3).was_called_with 42 |
| 89 | + |
| 90 | + pending "removing a callback multiple times doesn't interfere with other callbacks", -> |
| 91 | + input = InputCell 1 |
| 92 | + output = ComputeCell input, (x) -> x + 1 |
| 93 | + callback1 = spy.new(->) |
| 94 | + callback2 = spy.new(->) |
| 95 | + |
| 96 | + output\watch callback1 |
| 97 | + output\watch callback2 |
| 98 | + for i = 1, 10 |
| 99 | + output\unwatch callback1 |
| 100 | + input\set_value 2 |
| 101 | + |
| 102 | + assert.spy(callback1).was_called 0 |
| 103 | + assert.spy(callback2).was_called 1 |
| 104 | + assert.spy(callback2).was_called_with 3 |
| 105 | + |
| 106 | + pending 'callbacks only called once even if multiple inputs change', -> |
| 107 | + input = InputCell 1 |
| 108 | + plus_one = ComputeCell input, (x) -> x + 1 |
| 109 | + minus_one1 = ComputeCell input, (x) -> x - 1 |
| 110 | + minus_one2 = ComputeCell minus_one1, (x) -> x - 1 |
| 111 | + output = ComputeCell plus_one, minus_one2, (x, y) -> x * y |
| 112 | + callback = spy.new(->) |
| 113 | + |
| 114 | + output\watch callback |
| 115 | + input\set_value 4 |
| 116 | + assert.spy(callback).was_called 1 |
| 117 | + assert.spy(callback).was_called_with 10 |
| 118 | + |
| 119 | + pending "callbacks not called if inputs change but output doesn't", -> |
| 120 | + input = InputCell 1 |
| 121 | + plus_one = ComputeCell input, (x) -> x + 1 |
| 122 | + minus_one = ComputeCell input, (x) -> x - 1 |
| 123 | + always_two = ComputeCell plus_one, minus_one, (x, y) -> x - y |
| 124 | + callback = spy.new(->) |
| 125 | + |
| 126 | + always_two\watch callback |
| 127 | + input\set_value i for i = 1, 10 |
| 128 | + assert.spy(callback).was_called 0 |
0 commit comments