-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
translation-parameters-render.spec.ts
111 lines (94 loc) · 4.73 KB
/
translation-parameters-render.spec.ts
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
108
109
110
111
import { I18nConfiguration, TranslationBinding, TranslationParametersAttributePattern, TranslationParametersBindingCommand, TranslationParametersBindingInstruction, TranslationParametersBindingRenderer, TranslationParametersInstructionType } from '@aurelia/i18n';
import { AttrSyntax, BindingCommand, BindingCommandInstance, IAttributePattern, PlainAttributeSymbol } from '@aurelia/jit';
import { AttrBindingCommand } from '@aurelia/jit-html';
import { DI } from '@aurelia/kernel';
import { AnyBindingExpression, BindingType, ICallBindingInstruction, IController, IExpressionParser, IInstructionRenderer, IObserverLocator, IRenderContext, LifecycleFlags, RuntimeConfiguration } from '@aurelia/runtime';
import { DOM } from '@aurelia/runtime-html';
import { assert, TestContext } from '@aurelia/testing';
describe('TranslationParametersAttributePattern', function () {
function setup() {
const container = DI.createContainer();
container.register(TranslationParametersAttributePattern);
return container.get(IAttributePattern);
}
it('creates attribute syntax without `to`', function () {
const sut = setup();
const pattern = 't-params.bind';
const value = '{foo: "bar"}';
const actual: AttrSyntax = sut[pattern](pattern, value, []);
assert.equal(actual.command, pattern);
assert.equal(actual.rawName, pattern);
assert.equal(actual.rawValue, value);
assert.equal(actual.target, '');
});
});
describe('TranslationParametersBindingCommand', function () {
function setup() {
const container = DI.createContainer();
container.register(TranslationParametersBindingCommand);
return container.get<BindingCommandInstance>(BindingCommand.keyFrom(`t-params.bind`));
}
it('registers the `t-params.bind` command', function () {
const sut = setup();
assert.instanceOf(sut, TranslationParametersBindingCommand);
});
it('compiles the binding to a TranslationParametersBindingInstruction', function () {
const sut = setup();
const syntax: AttrSyntax = { command: 't-params.bind', rawName: 't-params.bind', rawValue: '{foo: "bar"}', target: '' };
const plainAttributesymbol: PlainAttributeSymbol = {
command: new AttrBindingCommand(),
flags: (void 0)!,
expression: { syntax } as unknown as AnyBindingExpression,
syntax
};
const actual = sut.compile(plainAttributesymbol);
assert.instanceOf(actual, TranslationParametersBindingInstruction);
});
});
describe('TranslationParametersBindingRenderer', function () {
function setup() {
const { container } = TestContext.createHTMLTestContext();
container.register(RuntimeConfiguration, I18nConfiguration);
return container as unknown as IRenderContext;
}
it('instantiated with instruction type', function () {
const container = setup();
const sut: IInstructionRenderer = new TranslationParametersBindingRenderer(container.get(IExpressionParser), container.get(IObserverLocator));
assert.equal(sut.instructionType, TranslationParametersInstructionType);
});
it('#render instantiates TranslationBinding if there are none existing', function () {
const container = setup();
const sut: IInstructionRenderer = new TranslationParametersBindingRenderer(container.get(IExpressionParser), container.get(IObserverLocator));
const expressionParser = container.get(IExpressionParser);
const renderable = ({} as unknown as IController);
const callBindingInstruction: ICallBindingInstruction = { from: expressionParser.parse('{foo: "bar"}', BindingType.BindCommand) } as unknown as ICallBindingInstruction;
sut.render(
LifecycleFlags.none,
DOM,
container,
renderable,
DOM.createElement('span'),
callBindingInstruction
);
assert.instanceOf(renderable.bindings[0], TranslationBinding);
});
it('#render add the paramExpr to the existing TranslationBinding for the target element', function () {
const container = setup();
const sut: IInstructionRenderer = new TranslationParametersBindingRenderer(container.get(IExpressionParser), container.get(IObserverLocator));
const expressionParser = container.get(IExpressionParser);
const targetElement = DOM.createElement('span');
const binding = new TranslationBinding(targetElement, container.get(IObserverLocator), container);
const renderable = ({ bindings: [binding] } as unknown as IController);
const paramExpr = expressionParser.parse('{foo: "bar"}', BindingType.BindCommand);
const callBindingInstruction: ICallBindingInstruction = { from: paramExpr } as unknown as ICallBindingInstruction;
sut.render(
LifecycleFlags.none,
DOM,
container,
renderable,
targetElement,
callBindingInstruction
);
assert.equal(binding.parametersExpr, paramExpr);
});
});