9 describe('Request.HTML', function(){
11 beforeEach(function(){
12 this.spy = jasmine.createSpy();
13 this.xhr = sinon.useFakeXMLHttpRequest();
14 var requests = this.requests = [];
15 this.xhr.onCreate = function(xhr){
24 it('should create an ajax request and pass the right arguments to the onComplete event', function(){
26 var response = '<body><img><div><span>res&ponsé</span></div><script>___SPEC___=5;</script></body>';
28 this.spy.identity = 'Request.HTML onComplete';
29 var request = new Request.HTML({
30 url: '../Helpers/request.php',
34 this.requests[0].respond(200, {'Content-Type': 'text/html'}, response);
36 expect(this.spy.wasCalled).toBe(true);
38 // checks arguments order
39 expect(this.spy).toHaveBeenCalledWith(request.response.tree, request.response.elements, request.response.html, request.response.javascript);
40 var onCompleteArgs = this.spy.argsForCall[0];
41 expect(onCompleteArgs[0][0].nodeName).toEqual('IMG');
42 expect(onCompleteArgs[0][1].nodeName).toEqual('DIV');
43 expect(onCompleteArgs[1][2].nodeName).toEqual('SPAN');
44 expect(onCompleteArgs[2]).toEqual('<img><div><span>res&ponsé</span></div>');
45 expect(onCompleteArgs[3].trim()).toEqual('___SPEC___=5;');
46 expect(___SPEC___).toEqual(5);
50 xit('(async) should create an ajax request and correctly generate the tree response from a tr', function(){
53 this.request = new Request.HTML({
54 url: '../Helpers/request.php',
57 '__response': '<tr><td>text</td></tr>', '__type': 'html'
61 waitsFor(800, function(){
62 return this.spy.wasCalled;
66 var onCompleteArgs = this.spy.argsForCall[0];
68 expect(onCompleteArgs[0][0].nodeName).toEqual('TR');
69 expect(onCompleteArgs[1][1].nodeName).toEqual('TD');
70 expect(onCompleteArgs[2]).toEqual('<tr><td>text</td></tr>');
75 xit('(async) should create an ajax request and correctly generate the tree response from options', function(){
78 this.request = new Request.HTML({
79 url: '../Helpers/request.php',
82 '__response': '<option>1</option><option>2</option><option>3</option>', '__type': 'html'
86 waitsFor(800, function(){
87 return this.spy.wasCalled;
91 var onCompleteArgs = this.spy.argsForCall[0];
93 expect(onCompleteArgs[0].length).toEqual(3);
94 expect(onCompleteArgs[1].length).toEqual(3);
95 expect(onCompleteArgs[2]).toEqual('<option>1</option><option>2</option><option>3</option>');
96 expect(onCompleteArgs[3]).toBeFalsy();
98 var firstOption = onCompleteArgs[0][0];
99 expect(firstOption.tagName).toEqual('OPTION');
100 expect(firstOption.innerHTML).toEqual('1');
105 it('should create an ajax request and correctly update an element with the response', function(){
107 var response = '<span>text</span>';
109 this.spy.identity = 'Request.HTML onComplete update element';
110 new Element('div', {'id': 'update', 'html': '<div>some</div>'}).inject(document.body);
111 this.request = new Request.HTML({
112 url: '../Helpers/request.php',
113 onComplete: this.spy,
117 this.requests[0].respond(200, {'Content-Type': 'text/html'}, response);
119 expect(this.spy.wasCalled).toBe(true);
121 var update = $('update');
122 expect(update.getChildren().length).toEqual(1);
123 expect(update.getFirst().get('tag')).toEqual('span');
124 expect(update.getFirst().get('text')).toEqual('text');
128 it('should create an ajax request and correctly append the response to an element', function(){
130 var response = '<div><span>text</span><p>paragraph</p></div>';
132 new Element('div', {'id': 'update', 'html': '<div>some</div>'}).inject(document.body);
133 this.spy.identity = 'Request.HTML onComplete ajax append';
134 this.request = new Request.HTML({
135 url: '../Helpers/request.php',
136 onComplete: this.spy,
140 this.requests[0].respond(200, {'Content-Type': 'text/html'}, response);
142 expect(this.spy.wasCalled).toBe(true);
144 var update = $('update');
145 expect(update.getChildren().length).toEqual(2);
146 expect(update.getFirst().get('tag')).toEqual('div');
147 expect(update.getFirst().get('text')).toEqual('some');
148 var div = update.getFirst().getNext();
149 expect(div.get('tag')).toEqual('div');
150 expect(div.getFirst().get('tag')).toEqual('span');
151 expect(div.getFirst().get('text')).toEqual('text');
152 expect(div.getLast().get('tag')).toEqual('p');
153 expect(div.getLast().get('text')).toEqual('paragraph');
158 it('should create an ajax request and correctly filter it by the passed selector', function(){
160 var response = '<span>text</span><a>aaa</a>';
162 this.spy.identity = 'Request.HTML onComplete filter';
163 var request = new Request.HTML({
164 url: '../Helpers/request.php',
165 onComplete: this.spy,
169 this.requests[0].respond(200, {'Content-Type': 'text/html'}, response);
170 expect(this.spy.wasCalled).toBe(true);
172 var onCompleteArgs = this.spy.argsForCall[0];
173 expect(onCompleteArgs[0].length).toEqual(1);
174 expect(onCompleteArgs[0][0].get('tag')).toEqual('a');
175 expect(onCompleteArgs[0][0].get('text')).toEqual('aaa');
179 it('should create an ajax request that filters the response and updates the target', function(){
181 var response = '<div>text<p><a>a link</a></p></div>';
183 this.spy.identity = 'Request.HTML onComplete update and filter';
184 new Element('div', {'id': 'update', 'html': '<div>some</div>'}).inject(document.body);
185 this.request = new Request.HTML({
186 url: '../Helpers/request.php',
187 onComplete: this.spy,
192 this.requests[0].respond(200, {'Content-Type': 'text/html'}, response);
193 expect(this.spy.wasCalled).toBe(true);
195 var update = $('update');
196 expect(update.getChildren().length).toEqual(1);
197 expect(update.getFirst().get('tag')).toEqual('a');
198 expect(update.getFirst().get('text')).toEqual('a link');
203 it('should create an ajax request that filters the response and appends to the target', function(){
205 var response = '<div>text<p><a>a link</a></p></div>';
207 new Element('div', {'id': 'update', 'html': '<div>some</div>'}).inject(document.body);
208 this.spy.identity = 'Request.HTML onComplete append and filter';
209 this.request = new Request.HTML({
210 url: '../Helpers/request.php',
211 onComplete: this.spy,
216 this.requests[0].respond(200, {'Content-Type': 'text/html'}, response);
217 expect(this.spy.wasCalled).toBe(true);
219 var update = $('update');
220 expect(update.getChildren().length).toEqual(2);
221 expect(update.get('html').toLowerCase()).toEqual('<div>some</div><a>a link</a>');
226 it('should create an ajax request through Element.load', function(){
228 var element = new Element('div');
230 var response = 'hello world!';
232 this.spy.identity = 'Request.HTML onComplete load';
233 var request = element.set('load', {
234 url: '../Helpers/request.php',
238 expect(instanceOf(request, Request.HTML)).toBeTruthy();
241 '__response': response, '__type': 'html'
244 this.requests[0].respond(200, {'Content-Type': 'text/html'}, response);
245 expect(this.spy.wasCalled).toBe(true);
248 var onCompleteArgs = this.spy.argsForCall[0];
249 expect(element.get('text')).toEqual('hello world!');