UNPKG

4.81 kBJavaScriptView Raw
1import { Container } from 'aurelia-dependency-injection';
2import { BindingLanguage } from '../src/binding-language';
3import { ResourceLoadContext, ViewCompileInstruction } from '../src/instructions';
4import { ViewCompiler } from '../src/view-compiler';
5import { ViewEngine } from '../src/view-engine';
6import { ViewResources } from '../src/view-resources';
7import { StaticViewStrategy } from '../src/view-strategy';
8import './setup';
9
10describe('ViewLocator', () => {
11 /**@type {ViewEngine} */
12 let viewEngine;
13 let container = new Container();
14 let appResources = new ViewResources();
15
16 beforeEach(() => {
17 let bindingLanguage = new class extends BindingLanguage {
18 createAttributeInstruction () {}
19 inspectAttribute (resources, tagName, attrName, attrValue) {
20 return { attrName, attrValue};
21 }
22 inspectTextContent () {}
23 };
24 container = new Container();
25 appResources = new ViewResources();
26 viewEngine = {
27 container: container,
28 appResources: appResources,
29 viewCompiler: new ViewCompiler(bindingLanguage, appResources)
30 };
31 });
32
33 describe('StaticViewStrategy', () => {
34 it('loads', (done) => {
35 let strategy = new StaticViewStrategy({
36 template: '<template><input value.bind="value" /></template>',
37 dependencies: []
38 });
39 class El {
40
41 }
42 strategy
43 .loadViewFactory(viewEngine, ViewCompileInstruction.normal, new ResourceLoadContext(), El)
44 .then((factory) => {
45 expect(factory.resources.getElement('el').target).toBe(El);
46 }).catch(ex => {
47 expect(ex.message).not.toContain('Cannot determine default view strategy for object.');
48 }).then(done);
49 });
50 });
51
52 it('loads dependencies', (done) => {
53 class EmmaFrost {
54 }
55 class AquaMan {
56 static $resource = {
57 type: 'attribute'
58 }
59 }
60 class VentureCapital {
61 static $resource = {
62 type: 'valueConverter'
63 }
64 }
65 class BabyBoomer {
66 static $resource = {
67 type: 'bindingBehavior'
68 }
69 }
70 class BlitzCrank {
71 static $resource = {
72 type: 'viewEngineHooks'
73 }
74
75 beforeCompile() {}
76 }
77 let strategy = new StaticViewStrategy({
78 template: '<template><input value.bind="value" /></template>',
79 dependencies: [AquaMan, VentureCapital, BabyBoomer, BlitzCrank]
80 });
81 strategy
82 .loadViewFactory(viewEngine, ViewCompileInstruction.normal, new ResourceLoadContext(), EmmaFrost)
83 .then((factory) => {
84 let resources = factory.resources;
85 expect(resources.getElement('emma-frost').target).toBe(EmmaFrost);
86 expect(resources.getAttribute('aqua-man').target).toBe(AquaMan);
87 expect(resources.getValueConverter('ventureCapital') instanceof VentureCapital).toBe(true);
88 expect(resources.getBindingBehavior('babyBoomer') instanceof BabyBoomer).toBe(true);
89 expect(resources.beforeCompile).toBe(true);
90 }).catch(ex => {
91 expect(ex.message).not.toContain('Cannot determine default view strategy for object.');
92 }).then(done);
93 });
94
95 it('loads async dependencies', done => {
96 class Ekko {}
97 class AureliaSol {
98 static $resource = {
99 type: 'attribute'
100 }
101 }
102 class Volibear {
103 static $resource = {
104 type: 'valueConverter'
105 }
106 }
107 class Braum {
108 static $resource = {
109 type: 'bindingBehavior'
110 }
111 }
112 class Thresh {
113 static $resource = {
114 type: 'viewEngineHooks'
115 }
116 beforeCompile() {}
117 }
118
119 function mockEsmImport(path) {
120 // Note: export name was intenionally made one character to demonstrate static dependencies declaration relies on
121 // the exported value (class), not the export name. This introduces inconsistency with the rest
122 return Promise.resolve({
123 b: Volibear,
124 c: Braum,
125 d: Thresh
126 });
127 }
128 let strategy = new StaticViewStrategy({
129 template: '<template><input value.bind="value" /></template>',
130 dependencies: () => [AureliaSol, mockEsmImport()]
131 });
132 strategy
133 .loadViewFactory(viewEngine, ViewCompileInstruction.normal, new ResourceLoadContext(), Ekko)
134 .then((factory) => {
135 let resources = factory.resources;
136 expect(resources.getElement('ekko').target).toBe(Ekko);
137 expect(resources.getAttribute('aurelia-sol').target).toBe(AureliaSol);
138 expect(resources.getValueConverter('volibear') instanceof Volibear).toBe(true);
139 expect(resources.getBindingBehavior('braum') instanceof Braum).toBe(true);
140 expect(resources.beforeCompile).toBe(true);
141 })
142 .catch(ex => {
143 expect(ex.message).not.toContain('Cannot determine default view strategy for object.');
144 }).then(done);
145 });
146});