-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathatomic-spec.js
More file actions
113 lines (95 loc) · 2.49 KB
/
atomic-spec.js
File metadata and controls
113 lines (95 loc) · 2.49 KB
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
/**
* atomic.js
*/
describe('atomic', function () {
/**
* xhr
*/
describe('xhr', function () {
beforeEach(function () {
spyOn(XMLHttpRequest.prototype, 'open').and.callThrough();
spyOn(XMLHttpRequest.prototype, 'send');
spyOn(XMLHttpRequest.prototype, 'setRequestHeader');
});
it('should open an XMLHttpRequest', function () {
atomic.ajax({
url: '/endpoint'
})
.success(function (data, xhr) {})
.error(function (data, xhr) {})
.always(function(data, xhr) {});
expect(XMLHttpRequest.prototype.open).toHaveBeenCalled();
});
it('should send and XMLHttpRequest', function () {
atomic.ajax({
url: '/endpoint'
})
.success(function (data, xhr) {})
.error(function (data, xhr) {});
expect(XMLHttpRequest.prototype.send).toHaveBeenCalled();
});
it('should set request header', function(){
atomic.ajax({
url: '/endpoint'
})
.success(function (data, xhr) {})
.error(function (data, xhr) {})
.always(function(data, xhr){});
expect(XMLHttpRequest.prototype.setRequestHeader).toHaveBeenCalled();
});
});
describe('always', function() {
it('should be called last after success or error', function(done) {
var result = 0;
atomic.ajax({
url: '/endpoint'
})
.always(function(data, xhr) {
result = 3;
})
.success(function(data, xhr) {
result = 1;
})
.error(function(data, xhr) {
result = 2;
});
setTimeout(function() {
expect(result).toEqual(3);
done();
}, 100);
});
});
describe('contentType', function(){
beforeEach(function(){
spyOn(XMLHttpRequest.prototype, 'setRequestHeader');
});
it('should use "application/x-www-form-urlencoded" as default Content-type', function(){
atomic.ajax({
url: '/endpoint'
});
expect(XMLHttpRequest.prototype.setRequestHeader)
.toHaveBeenCalledWith('Content-type', 'application/x-www-form-urlencoded');
});
it('should set Content-type', function() {
atomic.ajax({
url: '/endpoint',
headers: {
'Content-type': 'application/json'
}
});
expect(XMLHttpRequest.prototype.setRequestHeader)
.toHaveBeenCalledWith('Content-type', 'application/json');
});
it('should not be set when using FormData', function() {
atomic.ajax({
url: '/endpoint',
headers: {
'Content-type': 'application/json'
},
data: new FormData()
});
expect(XMLHttpRequest.prototype.setRequestHeader)
.not.toHaveBeenCalledWith('Content-type', 'application/json');
});
});
});