forked from denisnazarov/torii
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for postmessage in ui-service-mixin
- Loading branch information
Showing
1 changed file
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* eslint-disable ember/no-mixins, ember/no-new-mixins, ember/no-classic-classes */ | ||
|
||
import EmberObject from '@ember/object'; | ||
import UiServiceMixin, { | ||
CURRENT_REQUEST_KEY, | ||
} from 'torii/mixins/ui-service-mixin'; | ||
import PopupIdSerializer from 'torii/lib/popup-id-serializer'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Unit | Mixin | ui-service-mixin', function (hooks) { | ||
const originalWindowOpen = window.open; | ||
|
||
const popupId = '09123-asdf'; | ||
const expectedUrl = 'http://authServer'; | ||
const expectedRedirectUrl = 'http://localserver?code=fr'; | ||
const expectedMessage = 'getPendingRequestKey'; | ||
|
||
const buildMockWindow = function (windowName) { | ||
windowName = windowName || ''; | ||
window.addEventListener('message', (event) => { | ||
let msg; | ||
try { | ||
msg = JSON.parse(event.data); | ||
} catch { | ||
// allow | ||
} | ||
if (msg && Object.keys(msg)[0] === 'pendingRequestKey') { | ||
const obj = {}; | ||
const key = PopupIdSerializer.serialize(encodeURIComponent(popupId)); | ||
obj[key] = `${expectedUrl}?redirect_url=${expectedRedirectUrl}`; | ||
window.dispatchEvent( | ||
new MessageEvent('message', { | ||
data: JSON.stringify(obj), | ||
source: window, | ||
}) | ||
); | ||
} | ||
}); | ||
return { | ||
name: windowName, | ||
focus() {}, | ||
close() {}, | ||
open() { | ||
this.postMessage(expectedMessage); | ||
}, | ||
postMessage(msg) { | ||
window.dispatchEvent( | ||
new MessageEvent('message', { data: msg, source: window }) | ||
); | ||
}, | ||
}; | ||
}; | ||
|
||
const buildPopupIdGenerator = function (popupId) { | ||
return { | ||
generate() { | ||
return popupId; | ||
}, | ||
}; | ||
}; | ||
|
||
let Popup = EmberObject.extend(UiServiceMixin, { | ||
// Open a popup window. | ||
openRemote(url, pendingRequestKey) { | ||
this.remote = buildMockWindow(pendingRequestKey); | ||
this.remote.open(); | ||
}, | ||
|
||
close() { | ||
this.remote.close(); | ||
}, | ||
|
||
closeRemote() { | ||
this.remote.closed = true; | ||
}, | ||
|
||
pollRemote() { | ||
if (!this.remote) { | ||
return; | ||
} | ||
}, | ||
|
||
one() { | ||
this.closeRemote(); | ||
}, | ||
}); | ||
|
||
let popup; | ||
|
||
hooks.beforeEach(function () { | ||
popup = Popup.create({ remoteIdGenerator: buildPopupIdGenerator(popupId) }); | ||
localStorage.removeItem(CURRENT_REQUEST_KEY); | ||
}); | ||
|
||
hooks.afterEach(function () { | ||
localStorage.removeItem(CURRENT_REQUEST_KEY); | ||
window.open = originalWindowOpen; | ||
popup.destroy(); | ||
}); | ||
|
||
test('requests pending request key', function (assert) { | ||
let resultMessage; | ||
window.addEventListener('message', (event) => { | ||
resultMessage = event.data; | ||
}); | ||
|
||
popup.openRemote(expectedUrl, CURRENT_REQUEST_KEY); | ||
|
||
assert.strictEqual( | ||
resultMessage, | ||
expectedMessage, | ||
'requests pendingRequestKey' | ||
); | ||
}); | ||
|
||
test('returns data after receiving key', async function (assert) { | ||
const keys = ['redirect_url']; | ||
const result = await popup.open(expectedUrl, keys); | ||
console.log(result); | ||
|
||
assert.strictEqual( | ||
result.redirect_url, | ||
expectedRedirectUrl, | ||
'returns data successfully' | ||
); | ||
}); | ||
}); |