1 | var readFixtures = function() { |
---|
2 | return jasmine.getFixtures().proxyCallTo_('read', arguments); |
---|
3 | }; |
---|
4 | |
---|
5 | var loadFixtures = function() { |
---|
6 | jasmine.getFixtures().proxyCallTo_('load', arguments); |
---|
7 | }; |
---|
8 | |
---|
9 | var setFixtures = function(html) { |
---|
10 | jasmine.getFixtures().set(html); |
---|
11 | }; |
---|
12 | |
---|
13 | var sandbox = function(attributes) { |
---|
14 | return jasmine.getFixtures().sandbox(attributes); |
---|
15 | }; |
---|
16 | |
---|
17 | jasmine.getFixtures = function() { |
---|
18 | return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures(); |
---|
19 | }; |
---|
20 | |
---|
21 | jasmine.Fixtures = function() { |
---|
22 | this.containerId = 'jasmine-fixtures'; |
---|
23 | this.fixturesCache_ = {}; |
---|
24 | this.fixturesPath = 'spec/javascripts/fixtures'; |
---|
25 | }; |
---|
26 | |
---|
27 | jasmine.Fixtures.prototype.set = function(html) { |
---|
28 | this.cleanUp(); |
---|
29 | this.createContainer_(html); |
---|
30 | }; |
---|
31 | |
---|
32 | jasmine.Fixtures.prototype.load = function() { |
---|
33 | this.cleanUp(); |
---|
34 | this.createContainer_(this.read.apply(this, arguments)); |
---|
35 | }; |
---|
36 | |
---|
37 | jasmine.Fixtures.prototype.read = function() { |
---|
38 | var htmlChunks = []; |
---|
39 | |
---|
40 | var fixtureUrls = arguments; |
---|
41 | for(var urlCount = fixtureUrls.length, urlIndex = 0; urlIndex < urlCount; urlIndex++) { |
---|
42 | htmlChunks.push(this.getFixtureHtml_(fixtureUrls[urlIndex])); |
---|
43 | } |
---|
44 | |
---|
45 | return htmlChunks.join(''); |
---|
46 | }; |
---|
47 | |
---|
48 | jasmine.Fixtures.prototype.clearCache = function() { |
---|
49 | this.fixturesCache_ = {}; |
---|
50 | }; |
---|
51 | |
---|
52 | jasmine.Fixtures.prototype.cleanUp = function() { |
---|
53 | $('#' + this.containerId).remove(); |
---|
54 | }; |
---|
55 | |
---|
56 | jasmine.Fixtures.prototype.sandbox = function(attributes) { |
---|
57 | var attributesToSet = attributes || {}; |
---|
58 | return $('<div id="sandbox" />').attr(attributesToSet); |
---|
59 | }; |
---|
60 | |
---|
61 | jasmine.Fixtures.prototype.createContainer_ = function(html) { |
---|
62 | var container = $('<div id="' + this.containerId + '" />'); |
---|
63 | container.html(html); |
---|
64 | $('body').append(container); |
---|
65 | }; |
---|
66 | |
---|
67 | jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) { |
---|
68 | if (typeof this.fixturesCache_[url] == 'undefined') { |
---|
69 | this.loadFixtureIntoCache_(url); |
---|
70 | } |
---|
71 | return this.fixturesCache_[url]; |
---|
72 | }; |
---|
73 | |
---|
74 | jasmine.Fixtures.prototype.loadFixtureIntoCache_ = function(relativeUrl) { |
---|
75 | var self = this; |
---|
76 | var url = this.fixturesPath.match('/$') ? this.fixturesPath + relativeUrl : this.fixturesPath + '/' + relativeUrl; |
---|
77 | $.ajax({ |
---|
78 | async: false, // must be synchronous to guarantee that no tests are run before fixture is loaded |
---|
79 | cache: false, |
---|
80 | dataType: 'html', |
---|
81 | url: url, |
---|
82 | success: function(data) { |
---|
83 | self.fixturesCache_[relativeUrl] = data; |
---|
84 | } |
---|
85 | }); |
---|
86 | }; |
---|
87 | |
---|
88 | jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) { |
---|
89 | return this[methodName].apply(this, passedArguments); |
---|
90 | }; |
---|
91 | |
---|
92 | |
---|
93 | jasmine.JQuery = function() {}; |
---|
94 | |
---|
95 | jasmine.JQuery.browserTagCaseIndependentHtml = function(html) { |
---|
96 | return $('<div/>').append(html).html(); |
---|
97 | }; |
---|
98 | |
---|
99 | jasmine.JQuery.elementToString = function(element) { |
---|
100 | return $('<div />').append(element.clone()).html(); |
---|
101 | }; |
---|
102 | |
---|
103 | jasmine.JQuery.matchersClass = {}; |
---|
104 | |
---|
105 | |
---|
106 | (function(){ |
---|
107 | var jQueryMatchers = { |
---|
108 | toHaveClass: function(className) { |
---|
109 | return this.actual.hasClass(className); |
---|
110 | }, |
---|
111 | |
---|
112 | toBeVisible: function() { |
---|
113 | return this.actual.is(':visible'); |
---|
114 | }, |
---|
115 | |
---|
116 | toBeHidden: function() { |
---|
117 | return this.actual.is(':hidden'); |
---|
118 | }, |
---|
119 | |
---|
120 | toBeSelected: function() { |
---|
121 | return this.actual.is(':selected'); |
---|
122 | }, |
---|
123 | |
---|
124 | toBeChecked: function() { |
---|
125 | return this.actual.is(':checked'); |
---|
126 | }, |
---|
127 | |
---|
128 | toBeEmpty: function() { |
---|
129 | return this.actual.is(':empty'); |
---|
130 | }, |
---|
131 | |
---|
132 | toExist: function() { |
---|
133 | return this.actual.size() > 0; |
---|
134 | }, |
---|
135 | |
---|
136 | toHaveAttr: function(attributeName, expectedAttributeValue) { |
---|
137 | return hasProperty(this.actual.attr(attributeName), expectedAttributeValue); |
---|
138 | }, |
---|
139 | |
---|
140 | toHaveId: function(id) { |
---|
141 | return this.actual.attr('id') == id; |
---|
142 | }, |
---|
143 | |
---|
144 | toHaveHtml: function(html) { |
---|
145 | return this.actual.html() == jasmine.JQuery.browserTagCaseIndependentHtml(html); |
---|
146 | }, |
---|
147 | |
---|
148 | toHaveText: function(text) { |
---|
149 | return this.actual.text() == text; |
---|
150 | }, |
---|
151 | |
---|
152 | toHaveValue: function(value) { |
---|
153 | return this.actual.val() == value; |
---|
154 | }, |
---|
155 | |
---|
156 | toHaveData: function(key, expectedValue) { |
---|
157 | return hasProperty(this.actual.data(key), expectedValue); |
---|
158 | }, |
---|
159 | |
---|
160 | toBe: function(selector) { |
---|
161 | return this.actual.is(selector); |
---|
162 | }, |
---|
163 | |
---|
164 | toContain: function(selector) { |
---|
165 | return this.actual.find(selector).size() > 0; |
---|
166 | } |
---|
167 | }; |
---|
168 | |
---|
169 | var hasProperty = function(actualValue, expectedValue) { |
---|
170 | if (expectedValue === undefined) { |
---|
171 | return actualValue !== undefined; |
---|
172 | } |
---|
173 | return actualValue == expectedValue; |
---|
174 | }; |
---|
175 | |
---|
176 | var bindMatcher = function(methodName) { |
---|
177 | var builtInMatcher = jasmine.Matchers.prototype[methodName]; |
---|
178 | |
---|
179 | jasmine.JQuery.matchersClass[methodName] = function() { |
---|
180 | if (this.actual instanceof jQuery) { |
---|
181 | var result = jQueryMatchers[methodName].apply(this, arguments); |
---|
182 | this.actual = jasmine.JQuery.elementToString(this.actual); |
---|
183 | return result; |
---|
184 | } |
---|
185 | |
---|
186 | if (builtInMatcher) { |
---|
187 | return builtInMatcher.apply(this, arguments); |
---|
188 | } |
---|
189 | |
---|
190 | return false; |
---|
191 | }; |
---|
192 | }; |
---|
193 | |
---|
194 | for(var methodName in jQueryMatchers) { |
---|
195 | bindMatcher(methodName); |
---|
196 | } |
---|
197 | })(); |
---|
198 | |
---|
199 | beforeEach(function() { |
---|
200 | this.addMatchers(jasmine.JQuery.matchersClass); |
---|
201 | }); |
---|
202 | |
---|