1 | import test from 'ava';
|
2 | import td from 'testdouble';
|
3 | import Vue from 'vue';
|
4 |
|
5 | import Tree from './Tree.js';
|
6 | import Bridge from './Bridge.js';
|
7 | import Dispatcher from './Dispatcher.js';
|
8 |
|
9 |
|
10 |
|
11 | class Root {
|
12 | static get $trussMount() {return '/';}
|
13 | constructor() {
|
14 | this.x = 1;
|
15 | }
|
16 | get y() {
|
17 | return this.x + 1;
|
18 | }
|
19 | get z() {
|
20 | return this.a + 1;
|
21 | }
|
22 | get v() {
|
23 | return this.sub && this.sub.y + 10;
|
24 | }
|
25 | get w() {
|
26 | return this.sub && this.sub.z + 10;
|
27 | }
|
28 | makeA() {
|
29 | Vue.set(this, 'a', 2);
|
30 | }
|
31 | get complex() {
|
32 | return {b: this.x || 5};
|
33 | }
|
34 | get derived() {
|
35 | return this.complex.b + 1;
|
36 | }
|
37 | }
|
38 |
|
39 | class Subroot {
|
40 | static get $trussMount() {return '/sub';}
|
41 | get y() {
|
42 | return this.$parent.x + 2;
|
43 | }
|
44 | get z() {
|
45 | return this.$parent.a + 2;
|
46 | }
|
47 | }
|
48 |
|
49 | class SubrootFoo {
|
50 | static get $trussMount() {return '/sub/foo';}
|
51 | }
|
52 |
|
53 | test.beforeEach(t => {
|
54 | t.context = {
|
55 | rootUrl: 'https://example.firebaseio.com',
|
56 | bridge: td.instance(Bridge),
|
57 | dispatcher: td.instance(Dispatcher),
|
58 | truss: {get root() {return t.context.tree.root;}}
|
59 | };
|
60 | t.context.tree = new Tree(
|
61 | t.context.truss, t.context.rootUrl, t.context.bridge, t.context.dispatcher);
|
62 | t.context.tree.init([Root, SubrootFoo, Subroot]);
|
63 | });
|
64 |
|
65 | test.afterEach(t => {
|
66 | t.context.tree.destroy();
|
67 | });
|
68 |
|
69 | test('initialize placeholders', t => {
|
70 | const tree = t.context.tree;
|
71 | t.is(tree.root.constructor, Root);
|
72 | t.is(tree.root.sub.constructor, Subroot);
|
73 | t.is(tree.root.sub.foo.constructor, SubrootFoo);
|
74 | });
|
75 |
|
76 | test('update after instance property change', t => {
|
77 | const tree = t.context.tree;
|
78 | tree.root.x = 2;
|
79 | return Promise.resolve().then(() => {
|
80 | t.is(tree.root.y, 3);
|
81 | t.is(tree.root.sub.y, 4);
|
82 | t.is(tree.root.v, 14);
|
83 | });
|
84 | });
|
85 |
|
86 | test('update after new instance property created', t => {
|
87 | const tree = t.context.tree;
|
88 | tree.root.makeA();
|
89 | return Promise.resolve().then(() => {
|
90 | t.is(tree.root.z, 3);
|
91 | t.is(tree.root.sub.z, 4);
|
92 | t.is(tree.root.w, 14);
|
93 | });
|
94 | });
|
95 |
|
96 | test('computing non-primitive values', t => {
|
97 | const tree = t.context.tree;
|
98 | t.is(tree.root.derived, 2);
|
99 | tree.root.x = 3;
|
100 | return Promise.resolve().then(() => {
|
101 | t.is(tree.root.derived, 4);
|
102 | tree.checkVueObject(tree.root, '/');
|
103 | });
|
104 | });
|