UNPKG

4.92 kBJavaScriptView Raw
1import './setup';
2import {ViewCompiler} from '../src/view-compiler';
3import {ViewResources} from '../src/view-resources';
4
5class MockBindingLanguage {
6 inspectAttribute(resources, elementName, attrName, attrValue) {
7 }
8
9 createAttributeInstruction(resources, element, info, existingInstruction) {
10 }
11
12 inspectTextContent(resources, value) {
13 }
14}
15
16describe('ViewCompiler', () => {
17 var viewCompiler, language, resources;
18 beforeAll(() => {
19 language = new MockBindingLanguage();
20 viewCompiler = new ViewCompiler(language);
21 resources = new ViewResources(new ViewResources(), 'app.html');
22 });
23
24 describe('compile', () => {
25 it('compiles an empty template', () => {
26 var template = document.createElement('template'),
27 node = document.createDocumentFragment(),
28 factory;
29 template.appendChild(node);
30 factory = viewCompiler.compile(template, resources, null);
31 expect(factory).not.toBe(null);
32 });
33
34 it('throws on compile template within svg namespace', () => {
35 var template = document.createElementNS("http://www.w3.org/2000/svg", 'template'),
36 node = document.createDocumentFragment();
37 template.appendChild(node);
38
39 var compileFunc = () => {
40 viewCompiler.compile(template, resources, null)
41 };
42
43 expect(compileFunc).toThrow();
44 });
45 });
46
47 describe('compileNode', () => {
48 it('concatenates adjacent text nodes', () => {
49 var instructions = [], parentInjectorId = 'root', targetLightDOM = true,
50 node, parentNode;
51
52 parentNode = document.createElement('div');
53 node = document.createTextNode('Hello');
54 parentNode.appendChild(node);
55 parentNode.appendChild(document.createTextNode(' '));
56 parentNode.appendChild(document.createTextNode('World'));
57 parentNode.appendChild(document.createTextNode('!'));
58 spyOn(language, 'inspectTextContent');
59
60 node = viewCompiler._compileNode(node, resources, instructions, parentNode, parentInjectorId, targetLightDOM);
61 expect(language.inspectTextContent).toHaveBeenCalledWith(resources, 'Hello World!');
62 expect(node).toBe(null);
63 });
64
65 it('does not concatenate non-adjacent text nodes', () => {
66 var instructions = [], parentInjectorId = 'root', targetLightDOM = true,
67 node, parentNode, nextNode;
68
69 parentNode = document.createElement('div');
70 node = document.createTextNode('Hello');
71 parentNode.appendChild(node);
72 parentNode.appendChild(document.createTextNode(' '));
73 nextNode = document.createElement('em');
74 nextNode.textContent = 'World';
75 parentNode.appendChild(nextNode);
76 parentNode.appendChild(document.createTextNode('!'));
77 spyOn(language, 'inspectTextContent');
78
79 node = viewCompiler._compileNode(node, resources, instructions, parentNode, parentInjectorId, targetLightDOM);
80 expect(language.inspectTextContent).toHaveBeenCalledWith(resources, 'Hello ');
81 expect(node).toBe(nextNode);
82 });
83
84 it('clears class attributes containing interpolation expressions', () => {
85 var instructions = [], parentInjectorId = 'root', targetLightDOM = true,
86 node = document.createElement('div'), parentNode = null;
87 node.setAttribute('class', 'foo ${bar} baz');
88 spyOn(language, 'inspectAttribute').and.returnValue({
89 attrName: 'class',
90 expression: {attrToRemove: 'class'},
91 command: null
92 });
93 spyOn(language, 'createAttributeInstruction').and.returnValue({
94 attributes: {
95 'class': {
96 discrete: true,
97 attrToRemove: 'class'
98 }
99 }, attrName: 'class'
100 });
101 viewCompiler._compileNode(node, resources, instructions, parentNode, parentInjectorId, targetLightDOM);
102 expect(node.className).toBe('au-target');
103 });
104
105 it('does not clear class attributes with no interpolation expressions', () => {
106 var instructions = [], parentInjectorId = 'root', targetLightDOM = true,
107 node = document.createElement('div'), parentNode = null;
108
109 node.setAttribute('class', 'foo bar baz');
110 node.setAttribute('class.bind', 'someProperty');
111
112 spyOn(language, 'inspectAttribute').and.callFake((resources, attrName, attrValue) => {
113 if (attrName === 'class') {
114 return {attrName: 'class', expression: null, command: null}
115 } else {
116 return {attrName: 'class', expression: null, command: 'bind'};
117 }
118 });
119
120 spyOn(language, 'createAttributeInstruction').and.callFake((resources, node, info) => {
121 if (info.command) {
122 return {attributes: {'class': {discrete: true}}, attrName: 'class'};
123 } else {
124 return null;
125 }
126 });
127
128 viewCompiler._compileNode(node, resources, instructions, parentNode, parentInjectorId, targetLightDOM);
129 expect(node.className).toBe('foo bar baz au-target');
130 });
131
132 });
133
134});