-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
name-expression.spec.js
107 lines (98 loc) · 3.85 KB
/
name-expression.spec.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {
AccessScope,
AccessMember,
AccessKeyed,
LiteralPrimitive,
LiteralString,
BindingBehavior
} from '../src/ast';
import {createScopeForTest} from '../src/scope';
import {NameExpression} from '../src/name-expression';
describe('NameExpression', () => {
let element;
beforeEach(() => {
element = {
au: {
controller: {
view: {},
viewModel: {}
}
}
};
});
it('binds element to scope', () => {
let sourceExpression = new AccessScope('foo');
let expression = new NameExpression(sourceExpression, 'element');
let scope = createScopeForTest({});
let binding = expression.createBinding(element);
binding.bind(scope);
expect(scope.bindingContext.foo).toBe(element);
binding.unbind();
expect(scope.bindingContext.foo).toBe(null);
});
it('binds view to member', () => {
let sourceExpression = new AccessMember(new AccessScope('foo'), 'bar');
let expression = new NameExpression(sourceExpression, 'view');
let scope = createScopeForTest({ foo: {} });
let binding = expression.createBinding(element);
binding.bind(scope);
expect(scope.bindingContext.foo.bar).toBe(element.au.controller.view);
binding.unbind();
expect(scope.bindingContext.foo.bar).toBe(null);
});
it('binds view-model to key', () => {
let sourceExpression = new AccessKeyed(new AccessScope('foo'), new LiteralString('bar'));
let expression = new NameExpression(sourceExpression, 'view-model');
let scope = createScopeForTest({ foo: {} });
let binding = expression.createBinding(element);
binding.bind(scope);
expect(scope.bindingContext.foo['bar']).toBe(element.au.controller.viewModel);
binding.unbind();
expect(scope.bindingContext.foo['bar']).toBe(null);
});
it('binds controller to index', () => {
let sourceExpression = new AccessKeyed(new AccessScope('foo'), new LiteralPrimitive(5));
let expression = new NameExpression(sourceExpression, 'controller');
let scope = createScopeForTest({ foo: [] });
let binding = expression.createBinding(element);
binding.bind(scope);
expect(scope.bindingContext.foo[5]).toBe(element.au.controller);
binding.unbind();
expect(scope.bindingContext.foo[5]).toBe(null);
});
it('supports binding behaviors', () => {
let sourceExpression = new BindingBehavior(new AccessScope('foo'), 'test', []);
let testBehavior = {
bind: jasmine.createSpy('bind'),
unbind: jasmine.createSpy('unbind')
};
let lookupFunctions = {
bindingBehaviors: name => testBehavior,
valueConverters: name => null
};
let expression = new NameExpression(sourceExpression, 'element', lookupFunctions);
let scope = createScopeForTest({});
let binding = expression.createBinding(element);
binding.bind(scope);
expect(scope.bindingContext.foo).toBe(element);
expect(binding.lookupFunctions.bindingBehaviors().bind).toHaveBeenCalledWith(binding, scope);
binding.unbind();
expect(scope.bindingContext.foo).toBe(null);
expect(binding.lookupFunctions.bindingBehaviors().unbind).toHaveBeenCalledWith(binding, scope);
});
it('throws error with tagName when element has no behavior', () => {
let element = { tagName: 'test-element' };
expect(() => NameExpression.locateAPI(element, 'controller')).toThrow(new Error(`No Aurelia APIs are defined for the element: "${element.tagName}".`));
});
it('unbind preserves updated value', () => {
let sourceExpression = new AccessScope('foo');
let expression = new NameExpression(sourceExpression, 'element');
let scope = createScopeForTest({});
let binding = expression.createBinding(element);
binding.bind(scope);
expect(scope.bindingContext.foo).toBe(element);
scope.bindingContext.foo = 'should remain';
binding.unbind();
expect(scope.bindingContext.foo).toBe('should remain');
});
});