summaryrefslogtreecommitdiffstats
path: root/js/vendor/angular-ui/modules/directives/codemirror/test/codemirrorSpec.js
blob: 5e6bf3d3706fedb997c393715a3ea1c195a6bcb9 (plain)
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
/*global beforeEach, afterEach, describe, it, inject, expect, module, spyOn, CodeMirror, angular, $*/
/**
 * TODO Test all the CodeMirror events : cursorActivity viewportChange gutterClick focus blur scroll update.
 *      with  <textarea ui-codemirror="{onChange: doChange ,onCursorActivity: doSomething}" ng-model="foo">
 *
 */
describe('uiCodemirror', function () {
	'use strict';

	// declare these up here to be global to all tests
	var scope, $compile, $timeout, uiConfig = angular.module('ui.config');

	beforeEach(module('ui.directives'));
	beforeEach(function () {
		uiConfig.value('ui.config', {codemirror: {bar: 'baz'}});
	});

	// inject in angular constructs. Injector knows about leading/trailing underscores and does the right thing
	// otherwise, you would need to inject these into each test
	beforeEach(inject(function (_$rootScope_, _$compile_, _$timeout_) {
		scope = _$rootScope_.$new();
		$compile = _$compile_;
		$timeout = _$timeout_;
	}));

	afterEach(function () {
		uiConfig.value('ui.config', {}); // cleanup
	});

	describe('compiling this directive', function () {
		it('should throw an error if used against a non-textarea', function () {
			function compile() {
				$compile('<div ui-codemirror ng-model="foo"></div>')(scope);
			}

			expect(compile).toThrow();
		});

		it('should not throw an error when used against a textarea', function () {
			function compile() {
				$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
			}

			expect(compile).not.toThrow();
		});

		it('should throw an error when no ngModel attribute defined', function () {
			function compile() {
				$compile('<textarea ui-codemirror></textarea>')(scope);
			}

			expect(compile).toThrow();
		});

		it('should watch the uiCodemirror attribute', function () {
			spyOn(scope, '$watch');
			$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
			$timeout.flush();
			expect(scope.$watch).toHaveBeenCalled();
		});

	});

	describe('while spying on the CodeMirror instance', function () {

		var codemirror;

		beforeEach(function () {
			var fromTextArea = CodeMirror.fromTextArea;
			spyOn(CodeMirror, 'fromTextArea').andCallFake(function () {
				codemirror = fromTextArea.apply(this, arguments);
				return codemirror;
			});
		});

		describe('verify the directive options', function () {
			it('should include the passed options', function () {
				$compile('<textarea ui-codemirror="{oof: \'baar\'}" ng-model="foo"></textarea>')(scope);
				$timeout.flush();
				expect(CodeMirror.fromTextArea.mostRecentCall.args[1].oof).toEqual("baar");
			});

			it('should include the default options', function () {
				$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
				$timeout.flush();
				expect(CodeMirror.fromTextArea.mostRecentCall.args[1].bar).toEqual('baz');
			});
		});

		describe('when uiRefresh is added', function () {
			it('should trigger the CodeMirror.refresh() method', function () {
				$compile('<textarea ui-codemirror ng-model="foo" ui-refresh="bar"></textarea>')(scope);
				$timeout.flush();
				spyOn(codemirror, 'refresh');
				scope.$apply('bar = true');
				$timeout.flush();
				expect(codemirror.refresh).toHaveBeenCalled();
			});
		});


		describe('when the IDE changes', function () {
			it('should update the model', function () {
				$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
				scope.$apply("foo = 'bar'");
				$timeout.flush();
				var value = 'baz';
				codemirror.setValue(value);
				expect(scope.foo).toBe(value);
			});
		});

		describe('when the model changes', function () {
			it('should update the IDE', function () {
				var element = $compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
				scope.foo = 'bar';
				scope.$apply();
				$timeout.flush();
				expect(codemirror.getValue()).toBe(scope.foo);
			});
		});

		describe('when the model is undefined/null', function () {
			it('should update the IDE with an empty string', function () {
				var element = $compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
				scope.$apply();
				$timeout.flush();
				expect(scope.foo).toBe(undefined);
				expect(codemirror.getValue()).toBe('');
				scope.$apply('foo = "bar"');
				expect(scope.foo).toBe('bar');
				expect(codemirror.getValue()).toBe('bar');
				scope.$apply('foo = null');
				expect(scope.foo).toBe(null);
				expect(codemirror.getValue()).toBe('');
			});
		});
	});

	describe('when the model is an object or an array', function () {
		it('should throw an error', function () {
			function compileWithObject() {
				$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
				$timeout.flush();
				scope.foo = {};
				scope.$apply();
			}

			function compileWithArray() {
				$compile('<textarea ui-codemirror ng-model="foo"></textarea>')(scope);
				$timeout.flush();
				scope.foo = [];
				scope.$apply();
			}

			expect(compileWithObject).toThrow();
			expect(compileWithArray).toThrow();
		});
	});
});