UNPKG

3.83 kBJavaScriptView Raw
1import 'dom4';
2
3import 'angular';
4import 'angular-mocks';
5
6import proxyAttrs from './proxy-attrs';
7
8describe('Proxy attrs Ng', () => {
9 const ngModelValue = 'wow';
10 const ngDisabledValue = '!getDisabledState()';
11
12 it('should return untouched template if there in nothing to replace', () => {
13 const sourceTemplate = '<span><button data-proxy-ng-model>Some text</button><span>';
14 const replacedTemplate = proxyAttrs(sourceTemplate)({}, {$attr: {}});
15
16 replacedTemplate.should.be.equal(sourceTemplate);
17 });
18
19 it('should return template with replacement; last attribute', () => {
20 const sourceTemplate = '<span><button data-proxy-ng-model>Some text</button><span>';
21 const replacedTemplate = proxyAttrs(sourceTemplate)({}, {
22 ngModel: ngModelValue,
23 $attr: {ngModel: 'ng-model'}
24 });
25
26 replacedTemplate.should.
27 be.equal(sourceTemplate.replace('data-proxy-ng-model', `ng-model="${ngModelValue}"`));
28 });
29
30 it('should return template with replacement; middle attribute', () => {
31 const sourceTemplate = '<span><button data-proxy-ng-model class="">Some text</button><span>';
32 const replacedTemplate = proxyAttrs(sourceTemplate)({}, {
33 ngModel: ngModelValue,
34 $attr: {ngModel: 'ng-model'}
35 });
36
37 replacedTemplate.should.
38 be.equal(sourceTemplate.replace('data-proxy-ng-model', `ng-model="${ngModelValue}"`));
39 });
40
41 it('should return template with replacement; before new line', () => {
42 const sourceTemplate = '<span><button data-proxy-ng-model\n class="">Some text</button><span>';
43 const replacedTemplate = proxyAttrs(sourceTemplate)({}, {
44 ngModel: ngModelValue,
45 $attr: {ngModel: 'ng-model'}
46 });
47
48 replacedTemplate.should.
49 be.equal(sourceTemplate.replace('data-proxy-ng-model', `ng-model="${ngModelValue}"`));
50 });
51
52 it('should return template with replacement; with quotes', () => {
53 const sourceTemplate = '<span><button data-proxy-ng-model="">Some text</button><span>';
54 const replacedTemplate = proxyAttrs(sourceTemplate)({}, {
55 ngModel: ngModelValue,
56 $attr: {ngModel: 'ng-model'}
57 });
58
59 replacedTemplate.should.
60 be.equal(sourceTemplate.replace('data-proxy-ng-model=""', `ng-model="${ngModelValue}"`));
61 });
62
63 it('should respect whitelist instead if attrs', () => {
64 const sourceTemplate = '<span><button data-proxy-ng-model>Some text</button><span>';
65 const whitelist = [];
66 const replacedTemplate = proxyAttrs(sourceTemplate, whitelist)({}, {
67 ngModel: ngModelValue,
68 $attr: {ngModel: 'ng-model'}
69 });
70
71 replacedTemplate.should.be.equal(sourceTemplate);
72 });
73
74 it('should proxy multiple directives', () => {
75 const sourceTemplate = '<span><button data-proxy-ng-model>Some text</button><input type="text" data-proxy-ng-disabled></span>';
76
77 const replacedTemplate = proxyAttrs(sourceTemplate)({}, {
78 ngModel: ngModelValue,
79 ngDisabled: ngDisabledValue,
80 $attr: {ngModel: 'ng-model', ngDisabled: 'ng-disabled'}
81 });
82
83 const expectedTemplate = sourceTemplate.
84 replace('data-proxy-ng-model', `ng-model="${ngModelValue}"`).
85 replace('data-proxy-ng-disabled', `ng-disabled="${ngDisabledValue}"`);
86
87 replacedTemplate.should.be.equal(expectedTemplate);
88 });
89
90 it('should proxy one directive to multiple placeholders', () => {
91 const sourceTemplate = '<span><button data-proxy-ng-model>Some text</button><input type="text" data-proxy-ng-model></span>';
92
93 const replacedTemplate = proxyAttrs(sourceTemplate)({}, {
94 ngModel: ngModelValue,
95 ngDisabled: ngDisabledValue,
96 $attr: {ngModel: 'ng-model', ngDisabled: 'ng-disabled'}
97 });
98
99 const expectedTemplate = sourceTemplate.
100 replace(/data-proxy-ng-model/g, `ng-model="${ngModelValue}"`);
101
102 replacedTemplate.should.be.equal(expectedTemplate);
103 });
104});