diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2018-11-01 14:00:45 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2018-11-01 14:00:45 +0000 |
| commit | 63dd4d9613842b27a478c317d8d204d6e43e274e (patch) | |
| tree | 228f0405b19dad811976e646dcf7667af75a721b | |
| parent | fix component tests (diff) | |
| download | mmm-63dd4d9613842b27a478c317d8d204d6e43e274e.tar.gz mmm-63dd4d9613842b27a478c317d8d204d6e43e274e.zip | |
small fixes
| -rw-r--r-- | mmm/init.client.moon | 8 | ||||
| -rw-r--r-- | root/meta/test_component.moon | 175 |
2 files changed, 103 insertions, 80 deletions
diff --git a/mmm/init.client.moon b/mmm/init.client.moon index 144543b..c29a675 100644 --- a/mmm/init.client.moon +++ b/mmm/init.client.moon @@ -1,4 +1,4 @@ -export MODE, print, warn, relative, on_client +export MODE, print, warn, relative, on_load export window, document window = js.global @@ -43,7 +43,9 @@ relative = do name = base .. name if '.' == name\sub 1, 1 _require name -on_client = (f, ...) -> f ... - if on_load for f in *on_load do f! + +on_load = setmetatable {}, __newindex: (t, k, v) -> + rawset t, k, v + v! diff --git a/root/meta/test_component.moon b/root/meta/test_component.moon index 2c6c983..be44890 100644 --- a/root/meta/test_component.moon +++ b/root/meta/test_component.moon @@ -76,92 +76,113 @@ Fileder { reactive = ReactiveVar 'test' assert 'test' == reactive\get!, "expected x to be 'test'" - run_test "propagates updates", -> + run_test "provides #map shorthand", -> local done - reactive = ReactiveVar 'test' - reactive\subscribe coroutine.wrap (next) -> - assert next == 'toast', "expected next to be 'toast'" - assert coroutine.yield! == 'cheese', "expected next to be 'cheese'" - done = true + original = ReactiveVar 1 + mapped = original\map (a) -> a + 10 - reactive\set 'toast' - assert 'toast' == reactive\get!, "expected #get to return 'toast'" - reactive\set 'cheese' - assert done, "expected to reach the end" + assert mapped\get! == 11, "expected mapped to be 11" + return if MODE == 'SERVER' - run_test "passes old value as well", -> - local done + original\set 4 + assert mapped\get! == 14, "expected mapped to update" - reactive = ReactiveVar 1 - reactive\subscribe coroutine.wrap (next, last) -> - assert last == 1, "expected last:1 to be 1" - next, last = coroutine.yield! - assert last == 2, "expected last:2 to be 2" + mapped\subscribe coroutine.wrap (next, last) -> + assert next == 26, "expected next to be 26" + assert last == 14, "expected last to be 14" done = true - reactive\set 2 - reactive\set 3 + original\set 16 assert done, "expected to reach the end" - run_test "provides transform shorthand", -> - local done - - reactive = ReactiveVar 1 - reactive\subscribe coroutine.wrap (next, last) -> - assert last == 1, "expected last:1 to be 1" - next, last = coroutine.yield! - assert last == 2, "expected last:2 to be 2" - done = true - - add_one = (a) -> a + 1 - reactive\transform add_one - reactive\transform add_one - assert done, "expected to reach the end" - - run_test "#subscribe returns function to unsubscribe", -> - calls = 0 - - reactive = ReactiveVar 1 - unsub = reactive\subscribe coroutine.wrap () -> - calls += 1 - coroutine.yield! - calls += 1 - coroutine.yield! - calls += 1 - - assert 'function' == (type unsub), "expected to receive a function" - - reactive\set 2 - reactive\set 3 - assert calls == 2, "wat" - - unsub! - reactive\set 4 - assert calls == 2, "expected to stop receiving updates" - - run_test "tracks multiple subscriptions at once", -> - reactive = ReactiveVar 'test' - unsub = reactive\subscribe coroutine.wrap (next) -> - assert next == 'toast', "expected next to be toast" - next = coroutine.yield! - assert next == 'cheese', "expected next to be cheese" - coroutine.yield! - error "expected not to get here" - - reactive\set 'toast' - - local done - reactive\subscribe coroutine.wrap (next) -> - assert next == 'cheese', "expected next to be cheese" - next = coroutine.yield! - assert next == 'test', "expected next to be test" - done = true - - reactive\set 'cheese' - unsub! - reactive\set 'test' - assert done, "expected to reach the end" + if MODE == 'CLIENT' + run_test "propagates updates", -> + local done + + reactive = ReactiveVar 'test' + reactive\subscribe coroutine.wrap (next) -> + assert next == 'toast', "expected next to be 'toast'" + assert coroutine.yield! == 'cheese', "expected next to be 'cheese'" + done = true + + reactive\set 'toast' + assert 'toast' == reactive\get!, "expected #get to return 'toast'" + reactive\set 'cheese' + assert done, "expected to reach the end" + + run_test "passes old value as well", -> + local done + + reactive = ReactiveVar 1 + reactive\subscribe coroutine.wrap (next, last) -> + assert last == 1, "expected last:1 to be 1" + next, last = coroutine.yield! + assert last == 2, "expected last:2 to be 2" + done = true + + reactive\set 2 + reactive\set 3 + assert done, "expected to reach the end" + + run_test "provides #transform shorthand", -> + local done + + reactive = ReactiveVar 1 + reactive\subscribe coroutine.wrap (next, last) -> + assert last == 1, "expected last:1 to be 1" + next, last = coroutine.yield! + assert last == 2, "expected last:2 to be 2" + done = true + + add_one = (a) -> a + 1 + reactive\transform add_one + reactive\transform add_one + assert done, "expected to reach the end" + + run_test "#subscribe returns function to unsubscribe", -> + calls = 0 + + reactive = ReactiveVar 1 + unsub = reactive\subscribe coroutine.wrap () -> + calls += 1 + coroutine.yield! + calls += 1 + coroutine.yield! + calls += 1 + + assert 'function' == (type unsub), "expected to receive a function" + + reactive\set 2 + reactive\set 3 + assert calls == 2, "wat" + + unsub! + reactive\set 4 + assert calls == 2, "expected to stop receiving updates" + + run_test "tracks multiple subscriptions at once", -> + reactive = ReactiveVar 'test' + unsub = reactive\subscribe coroutine.wrap (next) -> + assert next == 'toast', "expected next to be toast" + next = coroutine.yield! + assert next == 'cheese', "expected next to be cheese" + coroutine.yield! + error "expected not to get here" + + reactive\set 'toast' + + local done + reactive\subscribe coroutine.wrap (next) -> + assert next == 'cheese', "expected next to be cheese" + next = coroutine.yield! + assert next == 'test', "expected next to be test" + done = true + + reactive\set 'cheese' + unsub! + reactive\set 'test' + assert done, "expected to reach the end" run_test = test_group 'ReactiveElement' |
