-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1024 from US-Trustee-Program/CAMS-476-logout-all-…
…tabs CAMS-476 - Add broadcast logout functionality
- Loading branch information
Showing
6 changed files
with
92 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,19 @@ | ||
export class BroadcastChannelHumble { | ||
private channel: BroadcastChannel; | ||
|
||
constructor(channelName: string) { | ||
this.channel = new BroadcastChannel(channelName); | ||
} | ||
|
||
onMessage(handler: (event: MessageEvent) => void) { | ||
this.channel.onmessage = handler; | ||
} | ||
|
||
postMessage(message: unknown) { | ||
this.channel.postMessage(message); | ||
} | ||
|
||
close() { | ||
this.channel.close(); | ||
} | ||
} |
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
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
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,48 @@ | ||
import { BroadcastChannelHumble } from '@/lib/humble/broadcast-channel-humble'; | ||
import { | ||
broadcastLogout, | ||
handleLogoutBroadcast, | ||
initializeBroadcastLogout, | ||
} from '@/login/broadcast-logout'; | ||
|
||
describe('Broadcast Logout', () => { | ||
test('should handle broadcast-logout properly', () => { | ||
const postMessageSpy = vi | ||
.spyOn(BroadcastChannelHumble.prototype, 'postMessage') | ||
.mockReturnValue(); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type | ||
let onMessageFn: Function = vi.fn(); | ||
const onMessageSpy = vi | ||
.spyOn(BroadcastChannelHumble.prototype, 'onMessage') | ||
.mockImplementation((arg) => { | ||
onMessageFn = arg; | ||
}); | ||
|
||
const closeSpy = vi.spyOn(BroadcastChannelHumble.prototype, 'close').mockReturnValue(); | ||
|
||
Object.defineProperty(global, 'window', Object.create(window)); | ||
global.window.location = { | ||
host: 'some-host', | ||
protocol: 'http:', | ||
assign: function (_url: string | URL): void {}, | ||
} as unknown as Location; | ||
const assignSpy = vi.spyOn(global.window.location, 'assign'); | ||
const expectedUrl = | ||
global.window.location.protocol + '//' + global.window.location.host + '/logout'; | ||
|
||
initializeBroadcastLogout(); | ||
broadcastLogout(); | ||
|
||
// Validate broadcastLogout function | ||
expect(postMessageSpy).toHaveBeenCalledWith('Logout all windows'); | ||
|
||
// Validate initializeBroadcastLogout function | ||
expect(onMessageSpy).toHaveBeenCalledWith(handleLogoutBroadcast); | ||
expect(onMessageFn).toEqual(handleLogoutBroadcast); | ||
|
||
// Validate handleLogout function | ||
onMessageFn(); | ||
expect(closeSpy).toHaveBeenCalled(); | ||
expect(assignSpy).toHaveBeenCalledWith(expectedUrl); | ||
}); | ||
}); |
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,20 @@ | ||
import { BroadcastChannelHumble } from '@/lib/humble/broadcast-channel-humble'; | ||
import { LOGOUT_PATH } from '@/login/login-library'; | ||
|
||
let channel: BroadcastChannelHumble; | ||
|
||
export function handleLogoutBroadcast() { | ||
const { host, protocol } = window.location; | ||
const logoutUri = protocol + '//' + host + LOGOUT_PATH; | ||
window.location.assign(logoutUri); | ||
channel?.close(); | ||
} | ||
|
||
export function initializeBroadcastLogout() { | ||
channel = new BroadcastChannelHumble('CAMS_logout'); | ||
channel.onMessage(handleLogoutBroadcast); | ||
} | ||
|
||
export function broadcastLogout() { | ||
channel?.postMessage('Logout all windows'); | ||
} |
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