Skip to content

Commit 417dcc8

Browse files
author
Dean Sofer
committed
Merge pull request angular-ui#81 from boneskull/moar-tests
Moar tests
2 parents e17d464 + 14b3116 commit 417dcc8

File tree

1 file changed

+86
-77
lines changed

1 file changed

+86
-77
lines changed
Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,118 @@
1-
/*global beforeEach, afterEach, describe, it, inject, expect, module, spyOn, CodeMirror*/
1+
/*global beforeEach, afterEach, describe, it, inject, expect, module, spyOn, CodeMirror, angular*/
22
describe('uiCodemirror', function () {
33
'use strict';
44

5-
var scope;
5+
var scope, $compile;
6+
beforeEach(function() {
7+
// throw some garbage in the codemirror cfg to be sure it's getting thru to the directive
8+
angular.module('ui.config').value('ui.config', {codemirror: {bar: 'baz'}});
9+
});
610
beforeEach(module('ui'));
7-
beforeEach(inject(function ($rootScope) {
8-
scope = $rootScope.$new();
11+
beforeEach(inject(function (_$rootScope_, _$compile_) {
12+
scope = _$rootScope_.$new();
13+
$compile = _$compile_;
914
}));
10-
afterEach(function () {
11-
scope.$destroy();
15+
16+
afterEach(function() {
17+
angular.module('ui.config').value('ui.config', {}); // cleanup
1218
});
19+
1320
describe('compiling this directive', function () {
1421
it('should throw an error if used against a non-textarea', function () {
15-
inject(function ($compile) {
16-
function compile() {
17-
$compile('<div ui-codemirror ng-model="foo"></div>')(scope);
18-
}
19-
20-
expect(compile).toThrow();
21-
});
22+
function compile() {
23+
$compile('<div ui-codemirror ng-model="foo"></div>')(scope);
24+
}
25+
expect(compile).toThrow();
2226
});
23-
it('should not throw an error when used against a textarea', function () {
24-
inject(function ($compile) {
25-
function compile() {
26-
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
27-
}
2827

29-
expect(compile).not.toThrow();
30-
});
28+
it('should not throw an error when used against a textarea', function () {
29+
function compile() {
30+
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
31+
}
32+
expect(compile).not.toThrow();
3133
});
32-
it('should throw an error when no ngModel attribute defined', function () {
33-
inject(function ($compile) {
34-
function compile() {
35-
$compile('<textarea ui-codemirror></textarea>')(scope);
36-
}
3734

38-
expect(compile).toThrow();
39-
});
35+
it('should throw an error when no ngModel attribute defined', function () {
36+
function compile() {
37+
$compile('<textarea ui-codemirror></textarea>')(scope);
38+
}
39+
expect(compile).toThrow();
4040
});
41+
4142
it('should watch the uiCodemirror attribute', function () {
42-
inject(function ($compile) {
43-
spyOn(scope, '$watch');
44-
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
45-
expect(scope.$watch).toHaveBeenCalled();
46-
});
43+
spyOn(scope, '$watch');
44+
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
45+
expect(scope.$watch).toHaveBeenCalled();
46+
});
47+
48+
it('should include the passed options', function () {
49+
spyOn(CodeMirror, 'fromTextArea');
50+
$compile('<textarea ui-codemirror="{foo: \'bar\'}" ng-model="foo"></textarea>')(scope);
51+
expect(CodeMirror.fromTextArea.mostRecentCall.args[1].foo).toEqual('bar');
52+
});
53+
54+
it('should include the default options', function () {
55+
spyOn(CodeMirror, 'fromTextArea');
56+
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
57+
expect(CodeMirror.fromTextArea.mostRecentCall.args[1].bar).toEqual('baz');
4758
});
4859
});
60+
4961
describe('when the model changes', function () {
5062
it('should update the IDE', function () {
51-
inject(function ($compile) {
52-
var element = $compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
53-
scope.foo = 'bar';
54-
scope.$apply();
55-
expect(element.siblings().text().trim()).toBe(scope.foo);
56-
});
63+
var element = $compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
64+
scope.foo = 'bar';
65+
scope.$apply();
66+
expect(element.siblings().text().trim()).toBe(scope.foo);
5767
});
5868
});
69+
5970
describe('when the IDE changes', function () {
6071
it('should update the model', function () {
61-
inject(function ($compile) {
62-
var codemirror,
63-
fromTextArea = CodeMirror.fromTextArea;
64-
spyOn(CodeMirror, 'fromTextArea').andCallFake(function () {
65-
codemirror = fromTextArea.apply(this, arguments);
66-
return codemirror;
67-
});
68-
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
69-
scope.foo = 'bar';
70-
scope.$apply();
71-
var value = 'baz';
72-
codemirror.setValue(value);
73-
expect(scope.foo).toBe(value);
72+
var codemirror,
73+
fromTextArea = CodeMirror.fromTextArea;
74+
spyOn(CodeMirror, 'fromTextArea').andCallFake(function () {
75+
codemirror = fromTextArea.apply(this, arguments);
76+
return codemirror;
7477
});
78+
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
79+
scope.foo = 'bar';
80+
scope.$apply();
81+
var value = 'baz';
82+
codemirror.setValue(value);
83+
expect(scope.foo).toBe(value);
7584
});
7685
});
86+
7787
describe('when the model is undefined/null', function () {
7888
it('should update the IDE with an empty string', function () {
79-
inject(function ($compile) {
80-
var element = $compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
81-
scope.$apply();
82-
expect(scope.foo).toBe(undefined);
83-
expect(element.siblings().text().trim()).toBe('');
84-
scope.foo = null;
85-
scope.$apply();
86-
expect(scope.foo).toBe(null);
87-
expect(element.siblings().text().trim()).toBe('');
88-
});
89+
var element = $compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
90+
scope.$apply();
91+
expect(scope.foo).toBe(undefined);
92+
expect(element.siblings().text().trim()).toBe('');
93+
scope.foo = null;
94+
scope.$apply();
95+
expect(scope.foo).toBe(null);
96+
expect(element.siblings().text().trim()).toBe('');
8997
});
9098
});
91-
describe('when the model is an object or an array', function() {
92-
it('should throw an error', function() {
93-
inject(function ($compile) {
94-
function compileWithObject() {
95-
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
96-
scope.foo = {};
97-
scope.$apply();
98-
}
99-
function compileWithArray() {
100-
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
101-
scope.foo = [];
102-
scope.$apply();
103-
}
104-
expect(compileWithObject).toThrow();
105-
expect(compileWithArray).toThrow();
106-
});
99+
100+
describe('when the model is an object or an array', function () {
101+
it('should throw an error', function () {
102+
function compileWithObject() {
103+
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
104+
scope.foo = {};
105+
scope.$apply();
106+
}
107+
108+
function compileWithArray() {
109+
$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
110+
scope.foo = [];
111+
scope.$apply();
112+
}
113+
114+
expect(compileWithObject).toThrow();
115+
expect(compileWithArray).toThrow();
107116
});
108117
});
109118
});

0 commit comments

Comments
 (0)