This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
async-widget-test.js
142 lines (122 loc) · 3.95 KB
/
async-widget-test.js
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
import Ember from 'ember';
import hbs from 'htmlbars-inline-precompile';
import { describeComponent, it } from 'ember-mocha';
import { beforeEach, afterEach } from 'mocha';
import { expect } from 'chai';
import AjaxService from 'ember-ajax/services/ajax';
import Pretender from 'pretender';
import { jsonFactory as json } from 'dummy/tests/helpers/json';
import wait from 'ember-test-helpers/wait';
const {
Component,
Service,
inject,
computed
} = Ember;
const PAYLOAD = { posts: [ { id: 1, title: 'hello world' } ] };
describeComponent(
'async-widget',
'AsyncWidgetComponent',
{
integration: true
},
function() {
beforeEach(function() {
this.server = new Pretender();
});
afterEach(function() {
this.server.shutdown();
});
it('service injected in component', function() {
this.server.get('/posts', json(200, PAYLOAD));
const authToken = 'foo';
this.register('service:session', Service.extend({ authToken }));
let receivedHeaders = [];
this.register('service:fajax', AjaxService.extend({
options() {
let options = this._super(...arguments);
Object.keys(options.headers).forEach((key) => {
receivedHeaders.push([key, options.headers[key]]);
});
return options;
},
session: inject.service(),
headers: computed('session.authToken', {
get() {
const headers = {};
let authToken = this.get('session.authToken');
if (authToken) {
headers.authToken = authToken;
}
return headers;
}
})
}));
let component;
this.register('component:async-widget', Component.extend({
url: null,
ajax: inject.service('fajax'),
didInsertElement() {
component = this;
},
loadData() {
const url = this.get('url');
return this.get('ajax').request(url);
},
helloStyle: computed('hello', {
get() {
return `hello ${this.get('hello')}`;
}
})
}));
this.render(hbs`{{async-widget id="async-widget" url="/posts"}}`);
return component.loadData().then(function(response) {
component.set('hello', 'world');
expect(component.get('helloStyle')).to.equal('hello world');
expect(receivedHeaders[0]).to.deep.equal(['authToken', 'foo']);
expect(response).to.deep.equal(PAYLOAD);
});
});
it.skip('error thrown in service can be caught in test', function() {
this.server.post('/posts/1', json(404, { error: 'not found' }), 200);
this.register('service:ajax', AjaxService.extend({
customPOST(url) {
return this.post(url);
}
}));
this.register('component:async-widget', Component.extend({
ajax: inject.service(),
click() {
this.get('ajax').customPOST(this.get('url'));
}
}));
this.render(
hbs`{{#async-widget classNames="async-widget" url="/posts/1"}}
Post!
{{/async-widget}}`
);
expect(function() {
this.$('.async-widget').click();
}).to.throw();
});
it('waiting for promises to complete', function() {
this.server.get('/foo', json(200, { foo: 'bar' }), 300);
this.register('component:async-widget', Component.extend({
layout: hbs`{{yield foo}}`,
ajax: inject.service(),
foo: 'foo',
click() {
this.get('ajax').request('/foo').then(({ foo }) => {
this.set('foo', foo);
});
}
}));
this.render(hbs`{{#async-widget classNames="async-widget" as |foo|}}Got: {{foo}} for foo{{/async-widget}}`);
expect(this.$('.async-widget').text()).to.equal('Got: foo for foo');
this.$('.async-widget').click();
return wait().then(() => {
expect(this.$('.async-widget').text()).to.equal('Got: bar for foo');
});
});
}
);