|
| 1 | +import { |
| 2 | + ngModelAttrsManipulator |
| 3 | +} |
| 4 | +from './../../src/helpers'; |
| 5 | + |
| 6 | +describe("formlyMaterial - ngModelAttrsManipulator", () => { |
| 7 | + it("should skip on skipNgModelAttrsManipulator", () => { |
| 8 | + const tpl = 'test'; |
| 9 | + const result = ngModelAttrsManipulator(tpl, { |
| 10 | + extras: { |
| 11 | + skipNgModelAttrsManipulator: true |
| 12 | + } |
| 13 | + }); |
| 14 | + |
| 15 | + expect(result).toBe(tpl); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should not modify on missing ngModel", () => { |
| 19 | + const tpl = 'test'; |
| 20 | + const result = ngModelAttrsManipulator(tpl, {}); |
| 21 | + |
| 22 | + expect(result).toBe(tpl); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should add attribute with value to ngModel", () => { |
| 26 | + const tpl = '<div><input ng-model="baz"/></div>'; |
| 27 | + const attr = { |
| 28 | + name: 'foo', |
| 29 | + value: 'bar' |
| 30 | + }; |
| 31 | + const result = ngModelAttrsManipulator(tpl, {}, attr.name, attr.value); |
| 32 | + const element = angular.element(result); |
| 33 | + |
| 34 | + expect(result).not.toBe(tpl); |
| 35 | + expect(element.find('[ng-model]').attr(attr.name)).toBe(attr.value); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should add attribute with value to few elements with ngModel", () => { |
| 39 | + const tpl = '<div><input ng-model="baz1"/><input ng-model="baz2"/></div>'; |
| 40 | + const attr = { |
| 41 | + name: 'foo', |
| 42 | + value: 'bar' |
| 43 | + }; |
| 44 | + const result = ngModelAttrsManipulator(tpl, {}, attr.name, attr.value); |
| 45 | + const element = angular.element(result); |
| 46 | + |
| 47 | + expect(result).not.toBe(tpl); |
| 48 | + expect(element.find('[ng-model]').length).toBe(2); |
| 49 | + expect(element.find('[ng-model]:eq(0)').attr(attr.name)).toBe(attr.value); |
| 50 | + expect(element.find('[ng-model]:eq(1)').attr(attr.name)).toBe(attr.value); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should not overwrite attribute on ngModel", () => { |
| 54 | + const name = 'baz'; |
| 55 | + const tpl = `<div><input ng-model="${name}" foo="${name}"/></div>`; |
| 56 | + const attr = { |
| 57 | + name: 'foo', |
| 58 | + value: 'bar' |
| 59 | + }; |
| 60 | + const result = ngModelAttrsManipulator(tpl, {}, attr.name, attr.value); |
| 61 | + const element = angular.element(result); |
| 62 | + |
| 63 | + expect(result).not.toBe(tpl); |
| 64 | + expect(element.find('[ng-model]').attr(attr.name)).toBe(name); |
| 65 | + }); |
| 66 | +}); |
0 commit comments