This repository was archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathangular-ui-tinymce.spec.js
184 lines (157 loc) · 5.66 KB
/
angular-ui-tinymce.spec.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*global describe, beforeEach, module, inject, it, spyOn, expect, $, angular, afterEach, runs, waits */
describe('uiTinymce', function () {
'use strict';
var scope, $compile, $timeout, element, directiveElement, id, text = '<p>Hello</p>';
beforeEach(module('ui.tinymce'));
beforeEach(function() {
// throw some garbage in the tinymce cfg to be sure it's getting thru to the directive
angular.module('ui.tinymce').value('uiTinymceConfig', {tinymce: {bar: 'baz'}});
});
beforeEach(inject(function(_$rootScope_, _$compile_, _$timeout_) {
scope = _$rootScope_.$new();
$compile = _$compile_;
$timeout = _$timeout_;
}));
afterEach(function() {
angular.module('ui.tinymce').value('uiTinymceConfig', {});
tinymce.remove('textarea');
});
/**
* Asynchronously runs the compilation.
*/
function compile() {
element = $compile('<form><textarea ui-tinymce="{foo: \'bar\', setup: setupFooBar }" data-ng-model="foo"></textarea></form>')(scope);
angular.element(document.getElementsByTagName('body')[0]).append(element);
scope.$apply();
$timeout.flush();
directiveElement = element.find('textarea');
id = directiveElement.attr('id');
}
it('should be pristine on load', function() {
compile();
expect(directiveElement.controller('form').$pristine).toBe(true);
expect(directiveElement.controller('ngModel').$pristine).toBe(true);
expect(directiveElement.controller('ngModel').$touched).toBe(false);
});
describe('compiling this directive', function() {
it('should include the passed options', function() {
spyOn(tinymce, 'init');
compile();
expect(tinymce.init).toHaveBeenCalled();
expect(tinymce.init.calls.mostRecent().args[0].foo).toBe('bar');
});
it('should include the default options', function() {
spyOn(tinymce, 'init');
compile();
expect(tinymce.init).toHaveBeenCalled();
expect(tinymce.init.calls.mostRecent().args[0].tinymce.bar).toBe('baz');
});
/*
it('should execute the passed `setup` option', function(done) {
scope.setupFooBar = jasmine.createSpy('setupFooBar');
compile();
//TODO: Get rid of timeout
setTimeout(function() {
expect(scope.setupFooBar).toHaveBeenCalled();
done();
}, 100);
}); */
});
it("should set touched on blur", function(){
compile();
expect(directiveElement.controller('ngModel').$touched).toBe(false);
element.find("textarea").triggerHandler("blur");
expect(directiveElement.controller('ngModel').$touched).toBe(true);
});
it('should remove tinymce instance on $scope destruction', function() {
compile();
expect(tinymce.get(element.attr('id'))).toBeDefined();
scope.$destroy();
expect(tinymce.get(element.attr('id'))).toBeNull();
});
// TODO: Figure out why such a large timeout is needed
describe('setting a value to the model', function() {
it('should update the editor', function(done) {
compile();
setTimeout(function() {
scope.foo = text;
scope.$apply();
try {
expect(tinymce.get(id).getContent()).toEqual(text);
} catch(e) {
expect(true).toBe(false);
done();
}
done();
}, 100);
});
it('shouldn\'t make the form dirty', function(done) {
compile();
setTimeout(function() {
scope.foo = text;
scope.$apply();
expect(directiveElement.controller('form').$dirty).toBe(false);
done();
});
});
// TODO: Fix test
/*xit('should handle undefined gracefully', function(done) {
compile();
setTimeout(function() {
scope.foo = undefined;
scope.$apply();
try {
expect(tinymce.get(id).getContent()).toEqual('');
} catch(e) {
expect(true).toBe(false);
done();
}
done();
}, 100);
});
xit('should handle null gracefully', function(done) {
compile();
setTimeout(function() {
scope.foo = null;
scope.$apply();
try {
expect(tinymce.get(id).getContent()).toEqual('');
} catch(e) {
expect(true).toBe(false);
done();
}
done();
}, 100);
});*/
});
/*describe('using the editor', function () {
it('should update the model', function (done) {
compile();
setTimeout(function () {
tinymce.get('foo').setContent(text);
expect(scope.foo).toEqual(text);
done();
});
});
});*/
it('should work with the ng-if directive', function () {
expect(function () {
$compile('<textarea ng-if="show" ui-tinymce data-ng-model="foo"></textarea>')(scope);
}).not.toThrow();
});
it('should create unique ID\'s when using multiple directives', function() {
element = $compile('<form><textarea ui-tinymce="{foo: \'bar\', setup: setupFooBar }" data-ng-model="foo_1"></textarea><textarea ui-tinymce="{foo: \'bar\', setup: setupFooBar }" data-ng-model="foo_2"></textarea><textarea ui-tinymce="{foo: \'bar\', setup: setupFooBar }" data-ng-model="foo_3"></textarea><textarea ui-tinymce="{foo: \'bar\', setup: setupFooBar }" data-ng-model="foo_4"></textarea></form>')(scope);
angular.element(document.getElementsByTagName('body')[0]).append(element);
scope.$apply();
$timeout.flush();
var directiveElements = element.find('textarea');
expect(directiveElements.length).toEqual(4);
var id1 = directiveElements[0].id;
var id2 = directiveElements[1].id;
var id3 = directiveElements[2].id;
var id4 = directiveElements[3].id;
expect(id1).not.toEqual(id2);
expect(id2).not.toEqual(id3);
expect(id3).not.toEqual(id4);
});
});