source: OKFNAnnotator (for Zope)/annotator_files/lib/vendor/jasmine-jquery.js @ 3:6356e78ccf5c

Last change on this file since 3:6356e78ccf5c was 3:6356e78ccf5c, checked in by casties, 12 years ago

new version contains Annotator JS files to be used with FilesystemSite?.

File size: 5.0 KB
Line 
1var readFixtures = function() {
2  return jasmine.getFixtures().proxyCallTo_('read', arguments);
3};
4
5var loadFixtures = function() {
6  jasmine.getFixtures().proxyCallTo_('load', arguments);
7};
8
9var setFixtures = function(html) {
10  jasmine.getFixtures().set(html);
11};
12
13var sandbox = function(attributes) {
14  return jasmine.getFixtures().sandbox(attributes);
15};
16
17jasmine.getFixtures = function() {
18  return jasmine.currentFixtures_ = jasmine.currentFixtures_ || new jasmine.Fixtures();
19};
20
21jasmine.Fixtures = function() {
22  this.containerId = 'jasmine-fixtures';
23  this.fixturesCache_ = {};
24  this.fixturesPath = 'spec/javascripts/fixtures';
25};
26
27jasmine.Fixtures.prototype.set = function(html) {
28  this.cleanUp();
29  this.createContainer_(html);
30};
31
32jasmine.Fixtures.prototype.load = function() {
33  this.cleanUp();
34  this.createContainer_(this.read.apply(this, arguments));
35};
36
37jasmine.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
48jasmine.Fixtures.prototype.clearCache = function() {
49  this.fixturesCache_ = {};
50};
51
52jasmine.Fixtures.prototype.cleanUp = function() {
53  $('#' + this.containerId).remove();
54};
55
56jasmine.Fixtures.prototype.sandbox = function(attributes) {
57  var attributesToSet = attributes || {};
58  return $('<div id="sandbox" />').attr(attributesToSet);
59};
60
61jasmine.Fixtures.prototype.createContainer_ = function(html) {
62  var container = $('<div id="' + this.containerId + '" />');
63  container.html(html);
64  $('body').append(container);
65};
66
67jasmine.Fixtures.prototype.getFixtureHtml_ = function(url) { 
68  if (typeof this.fixturesCache_[url] == 'undefined') {
69    this.loadFixtureIntoCache_(url);
70  }
71  return this.fixturesCache_[url];
72};
73
74jasmine.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
88jasmine.Fixtures.prototype.proxyCallTo_ = function(methodName, passedArguments) {
89  return this[methodName].apply(this, passedArguments);
90};
91
92
93jasmine.JQuery = function() {};
94
95jasmine.JQuery.browserTagCaseIndependentHtml = function(html) {
96  return $('<div/>').append(html).html();
97};
98
99jasmine.JQuery.elementToString = function(element) {
100  return $('<div />').append(element.clone()).html();
101};
102
103jasmine.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
199beforeEach(function() {
200  this.addMatchers(jasmine.JQuery.matchersClass);
201});
202
Note: See TracBrowser for help on using the repository browser.