Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit 2453d79

Browse files
author
Daniel Brain
committed
Improved tests
1 parent 933f03e commit 2453d79

File tree

3 files changed

+119
-50
lines changed

3 files changed

+119
-50
lines changed

src/interface/server.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ export function listen(options) {
1818
};
1919

2020
if (options.once) {
21-
options.handler = util.once(options.handler);
21+
let handler = options.handler;
22+
options.handler = util.once(function() {
23+
removeRequestListener(options)
24+
return handler.apply(this, arguments);
25+
});
2226
}
2327

2428
let override = options.override || CONFIG.MOCK_MODE;

test/child.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ window.console.karma = function() {
88
};
99

1010
postRobot.on('sendMessageToParent', function(source, data) {
11-
postRobot.sendToParent(data.messageName);
11+
return postRobot.sendToParent(data.messageName, data.data);
1212
});
1313

1414
postRobot.on('setupListener', function(source, data) {
15-
postRobot.on(data.messageName, { override: true }, function() {
15+
postRobot.once(data.messageName, function() {
1616
return data.data;
1717
});
1818
});

test/test.js

+112-47
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var childWindow = createPopup('child.htm');
2626

2727
var otherChildFrame = createIframe('child.htm');
2828

29-
describe('post-robot', function() {
29+
describe('[post-robot] happy cases', function() {
3030

3131
it('should set up a simple server and listen for a request', function(done) {
3232

@@ -75,26 +75,6 @@ describe('post-robot', function() {
7575
}).catch(done);
7676
});
7777

78-
it('should get an error when messaging with an unknown name', function() {
79-
80-
return postRobot.send(childFrame, 'doesntexist').then(function(data) {
81-
throw new Error('Expected success handler to not be called');
82-
}, function(err) {
83-
assert.ok(err);
84-
});
85-
});
86-
87-
it('should get a callback error when messaging with an unknown name', function(done) {
88-
89-
postRobot.send(childFrame, 'doesntexist', function(err, data) {
90-
assert.ok(err);
91-
if (data) {
92-
throw new Error('Expected data to be blank');
93-
}
94-
done();
95-
});
96-
});
97-
9878
it('should pass a function across windows and be able to call it later', function(done) {
9979

10080
postRobot.send(childFrame, 'setupListener', {
@@ -112,7 +92,9 @@ describe('post-robot', function() {
11292
});
11393
});
11494

115-
it('should work when referencing the child by id', function() {
95+
it.skip('should be able to proxy messsages from one window to another', function() {
96+
97+
postRobot.proxy(childFrame, otherChildFrame);
11698

11799
return postRobot.send(childFrame, 'setupListener', {
118100

@@ -123,15 +105,23 @@ describe('post-robot', function() {
123105

124106
}).then(function() {
125107

126-
return postRobot.send('childframe', 'foo').then(function(data) {
108+
return postRobot.send(otherChildFrame, 'sendMessageToParent', {
109+
messageName: 'foo',
110+
111+
112+
}).then(function(data) {
127113
assert.equal(data.foo, 'bar');
128114
});
129115
});
130116
});
117+
});
131118

132-
it('should work with a child window', function() {
133119

134-
return postRobot.send(childWindow, 'setupListener', {
120+
describe('[post-robot] options', function() {
121+
122+
it('should work when referencing the child by id', function() {
123+
124+
return postRobot.send(childFrame, 'setupListener', {
135125

136126
messageName: 'foo',
137127
data: {
@@ -140,7 +130,7 @@ describe('post-robot', function() {
140130

141131
}).then(function() {
142132

143-
return postRobot.send(childWindow, 'foo').then(function(data) {
133+
return postRobot.send('childframe', 'foo').then(function(data) {
144134
assert.equal(data.foo, 'bar');
145135
});
146136
});
@@ -159,28 +149,43 @@ describe('post-robot', function() {
159149
}).then(function() {
160150
return postRobot.send(childFrame, 'sendMessageToParent', {
161151
messageName: 'foobu'
162-
})
163-
}).then(function() {
164-
assert.equal(count, 1);
152+
}).then(function() {
153+
throw new Error('Expected success handler to not be called');
154+
}, function() {
155+
assert.equal(count, 1);
156+
});
165157
});
166158
});
167159

168-
it('should error out if you try to register the same listener name twice', function() {
160+
it.skip('should be able to re-register the same once handler after the first is called', function() {
169161

170-
postRobot.on('onceonly', function() {
171-
// pass
162+
var count = 0;
163+
164+
postRobot.once('foobu', { override: true }, function(source, data) {
165+
count += data.add;
172166
});
173167

174-
try {
175-
postRobot.on('onceonly', function() {
176-
// pass
168+
return postRobot.send(childFrame, 'sendMessageToParent', {
169+
messageName: 'foobu',
170+
data: {
171+
add: 2
172+
}
173+
}).then(function() {
174+
175+
postRobot.once('foobu', { override: true }, function() {
176+
count += data.add;
177177
});
178-
} catch (err) {
179-
assert.ok(err);
180-
return;
181-
}
182178

183-
throw new Error('Expected error handler to be called');
179+
return postRobot.send(childFrame, 'sendMessageToParent', {
180+
messageName: 'foobu',
181+
data: {
182+
add: 3
183+
}
184+
});
185+
186+
}).then(function() {
187+
assert.equal(count, 5);
188+
});
184189
});
185190

186191
it('should allow you to register the same listener twice providing it is to different windows', function() {
@@ -207,19 +212,60 @@ describe('post-robot', function() {
207212
}).then(function() {
208213
return postRobot.send(childFrame, 'sendMessageToParent', {
209214
messageName: 'specificchildlistener'
210-
})
211-
}).then(function() {
212-
assert.equal(count, 1);
215+
}).then(function() {
216+
throw new Error('Expected success handler to not be called');
217+
}, function(err) {
218+
assert.ok(err);
219+
assert.equal(count, 1);
220+
});
213221
});
214222
});
223+
});
224+
225+
226+
describe('[post-robot] error cases', function() {
227+
228+
it('should get an error when messaging with an unknown name', function() {
229+
230+
return postRobot.send(childFrame, 'doesntexist').then(function(data) {
231+
throw new Error('Expected success handler to not be called');
232+
}, function(err) {
233+
assert.ok(err);
234+
});
235+
});
236+
237+
it('should get a callback error when messaging with an unknown name', function(done) {
238+
239+
postRobot.send(childFrame, 'doesntexist', function(err, data) {
240+
assert.ok(err);
241+
if (data) {
242+
throw new Error('Expected data to be blank');
243+
}
244+
done();
245+
});
246+
});
247+
248+
it('should error out if you try to register the same listener name twice', function() {
249+
250+
postRobot.on('onceonly', function() {
251+
// pass
252+
});
215253

216-
/*
254+
try {
255+
postRobot.on('onceonly', function() {
256+
// pass
257+
});
258+
} catch (err) {
259+
assert.ok(err);
260+
return;
261+
}
262+
263+
throw new Error('Expected error handler to be called');
264+
});
217265

218266
it('should fail when postMessage or global methods are not available', function(done) {
219267

220-
delete window.postMessage;
221268
delete window.__postRobot__;
222-
delete window.frames;
223269

224270
Object.defineProperty(window, 'postMessage', {
225271
value: function() {
@@ -235,6 +281,25 @@ describe('post-robot', function() {
235281
messageName: 'nowayin'
236282
});
237283
});
284+
});
285+
286+
287+
describe('[post-robot] popup tests', function() {
288+
289+
it('should work with a child window', function() {
238290

239-
*/
291+
return postRobot.send(childWindow, 'setupListener', {
292+
293+
messageName: 'foo',
294+
data: {
295+
foo: 'bar'
296+
}
297+
298+
}).then(function() {
299+
300+
return postRobot.send(childWindow, 'foo').then(function(data) {
301+
assert.equal(data.foo, 'bar');
302+
});
303+
});
304+
});
240305
});

0 commit comments

Comments
 (0)