-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockhelper_test.js
More file actions
150 lines (136 loc) · 4.47 KB
/
mockhelper_test.js
File metadata and controls
150 lines (136 loc) · 4.47 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
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
143
144
145
146
147
148
149
150
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Tests for mockhelper.js
*/
goog.provide('firebaseui.auth.MockHelperTest');
goog.require('firebaseui.auth.testing.MockHelper');
goog.require('goog.testing.jsunit');
goog.setTestOnly('firebaseui.auth.MockHelperTest');
var myMock;
function setUp() {
var ref1 = {};
var ref2 = {};
var asyncMethods = {
'METHOD_1': {
'name': 'method1',
'context': ref1
},
'METHOD_2': {
'name': 'method2',
'context': ref1
},
'METHOD_3': {
'name': 'method3',
'context': ref2
},
'METHOD_4': {
'name': 'method4',
'context': ref2
},
};
myMock = new firebaseui.auth.testing.MockHelper(asyncMethods);
myMock['ref1'] = ref1;
myMock['ref2'] = ref2;
}
function testMockHelper_success() {
var expectedError1 = new Error('foo');
var expectedError2 = new Error('bar');
myMock.install();
var marker = 0;
myMock['ref1'].method1(true, {'a': 1}).then(function(result) {
marker++;
assertEquals('success', result);
return myMock['ref2'].method3();
}).then(function(result) {
marker++;
assertFalse(result);
return myMock['ref1'].method2('something');
}).thenCatch(function(error) {
assertEquals(expectedError1, error);
return myMock['ref2'].method4(10);
}).thenCatch(function(error) {
assertEquals(expectedError2, error);
return myMock['ref1'].method1();
});
// method1 already called before assertMethod1.
var method1Assert = myMock['ref1'].assertMethod1([true, {'a': 1}], 'success');
// assertMethod3 called before method3.
var method3Assert =
myMock['ref2'].assertMethod3([], function() {return false;});
method1Assert.then(function() {
// method1Assert should resolve before method1 resolves.
assertEquals(0, marker);
});
method3Assert.then(function() {
// method3Assert should resolve before method3 resolves.
assertEquals(1, marker);
});
// method1 already called. This should resolve event though it is not yet
// processed.
return method1Assert.then(function() {
// Confirm, method1Assert resolves before method1 resolves.
assertEquals(0, marker);
return myMock.process();
}).then(function() {
// Process would resolve method1 and method3.
assertEquals(2, marker);
}).then(function() {
// These asserts can also be chained with process.
myMock['ref1'].assertMethod2(['something'], null, expectedError1);
myMock['ref2'].assertMethod4(
[10], null, function() {return expectedError2;});
myMock['ref1'].assertMethod1([]);
return myMock.process().then(function() {
return myMock.uninstall();
});
});
}
function testMockHelper_uncalledApiError() {
// Asserting a different API than the one actually called.
myMock.install();
myMock['ref1'].method1(true, {'a': 1}).then(function(result) {
assertEquals('success', result);
return myMock['ref2'].method3();
});
// This will never get called and will result in a missing API request
// error.
myMock['ref2'].assertMethod3([], function() {return false;});
return myMock.process().then(function() {
return myMock.uninstall();
}).thenCatch(function(e) {
assertEquals(
'missing API request: method3', e.message);
});
}
function testMockHelper_unexpectedApiError() {
// Forgetting to assert API calls that were actually called.
myMock.install();
myMock['ref1'].method1(true, {'a': 1}).then(function(result) {
assertEquals('success', result);
// This unexpect API call will throw an error.
return myMock['ref2'].method3();
}, function(result) {
assertFalse(result);
return myMock['ref1'].method2('something');
});
// Simulate method1 API call only.
myMock['ref1'].assertMethod1([true, {'a': 1}], 'success');
return myMock.process().then(function() {
return myMock.uninstall();
}).thenCatch(function(e) {
assertEquals(
'unexpected API request(s): method3', e.message);
});
}