-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
111 additions
and
57 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 |
---|---|---|
@@ -1,91 +1,137 @@ | ||
'use strict'; | ||
|
||
// @ts-nocheck | ||
|
||
import * as popup from '../src/utils/popup'; | ||
import { ENDPOINTS } from '../src/config/config'; | ||
import { ForceMutable } from './utils'; | ||
|
||
const TEST_URL_INVALID = 'invalid url'; | ||
const TEST_URL_VALID = ENDPOINTS.SPiD.DEV; | ||
|
||
const SCREEN: Screen = { | ||
availHeight: 0, | ||
availWidth: 0, | ||
colorDepth: 0, | ||
height: 0, | ||
orientation: screen.orientation, | ||
pixelDepth: 0, | ||
width: 0, | ||
}; | ||
|
||
describe('Popup — open', () => { | ||
let mockWindow: ForceMutable<Window>; | ||
beforeEach(() => { | ||
// shallow copy of window | ||
mockWindow = { ...window }; | ||
}); | ||
|
||
const defaultFeatures = 'scrollbars=yes,location=yes,status=no,menubar=no,toolbar=no,resizable=yes'; | ||
|
||
test('Fails if window undefined', () => { | ||
// @ts-expect-error | ||
expect(() => popup.open()) | ||
.toThrow(/window was supposed to be an object but it is undefined/); | ||
}); | ||
|
||
test('Fails if window lacks "screen"', () => { | ||
const window = {}; | ||
expect(() => popup.open(window)) | ||
delete (mockWindow as Partial<ForceMutable<Window>>).screen; | ||
|
||
expect(() => popup.open(mockWindow, TEST_URL_VALID)) | ||
.toThrow(/window should be a valid Window object but it lacks a 'screen' property/); | ||
}); | ||
|
||
test('Fails if window lacks "open"', () => { | ||
const window = { screen: {} }; | ||
expect(() => popup.open(window)) | ||
mockWindow.screen = SCREEN; | ||
delete (mockWindow as Partial<ForceMutable<Window>>).open; | ||
|
||
expect(() => popup.open(mockWindow, TEST_URL_VALID)) | ||
.toThrow(/window should be a valid Window object but it lacks an 'open' function/); | ||
}); | ||
|
||
test('Fails if url not valid', () => { | ||
const window = { screen: {}, open: () => {} }; | ||
expect(() => popup.open(window)).toThrow(/Invalid URL for popup/); | ||
mockWindow.screen = SCREEN; | ||
|
||
expect(() => popup.open(mockWindow, TEST_URL_INVALID)).toThrow(/Invalid URL for popup/); | ||
}); | ||
|
||
test('Works with valid window and url', () => { | ||
return new Promise((resolve) => { | ||
const open = (url, windowName, features) => { | ||
expect(url).toBe('http://example.com'); | ||
const open = (url: string, windowName: string, features: string) => { | ||
expect(url).toBe(TEST_URL_VALID); | ||
expect(windowName).toBe(''); | ||
expect(features) | ||
.toBe(defaultFeatures); | ||
resolve(); | ||
resolve(''); | ||
}; | ||
const window = { screen: {}, open }; | ||
popup.open(window, 'http://example.com'); | ||
const spy = jest.fn().mockImplementation(open); | ||
|
||
mockWindow.screen = SCREEN; | ||
mockWindow.open = spy; | ||
popup.open(mockWindow, TEST_URL_VALID); | ||
}); | ||
}); | ||
|
||
test('Works with valid window, url and windowName', () => { | ||
const EXPECTED_WINDOW_NAME = 'FooBar'; | ||
|
||
return new Promise((resolve) => { | ||
const open = (url, windowName, features) => { | ||
expect(url).toBe('http://example.com'); | ||
expect(windowName).toBe('FooBar'); | ||
const open = (url: string, windowName: string, features: string) => { | ||
expect(url).toBe(TEST_URL_VALID); | ||
expect(windowName).toBe(EXPECTED_WINDOW_NAME); | ||
expect(features) | ||
.toBe(defaultFeatures); | ||
resolve(); | ||
resolve(''); | ||
}; | ||
const window = { screen: {}, open }; | ||
popup.open(window, 'http://example.com', 'FooBar'); | ||
const spy = jest.fn().mockImplementation(open); | ||
|
||
mockWindow.screen = SCREEN; | ||
mockWindow.open = spy; | ||
popup.open(mockWindow, TEST_URL_VALID, EXPECTED_WINDOW_NAME); | ||
}); | ||
}); | ||
|
||
test('Works with valid window, url, windowName and features', () => { | ||
const EXPECTED_WINDOW_NAME = 'FooBar'; | ||
const FEATURES = { foo: 'bar' }; | ||
|
||
return new Promise((resolve) => { | ||
const open = (url, windowName, features) => { | ||
expect(url).toBe('http://example.com'); | ||
expect(windowName).toBe('FooBar'); | ||
const open = (url: string, windowName: string, features: string) => { | ||
expect(url).toBe(TEST_URL_VALID); | ||
expect(windowName).toBe(EXPECTED_WINDOW_NAME); | ||
expect(features) | ||
.toBe(defaultFeatures + ',foo=bar'); | ||
resolve(); | ||
resolve(''); | ||
}; | ||
const window = { screen: {}, open }; | ||
popup.open(window, 'http://example.com', 'FooBar', { foo: 'bar' }); | ||
const spy = jest.fn().mockImplementation(open); | ||
|
||
mockWindow.screen = SCREEN; | ||
mockWindow.open = spy; | ||
popup.open(mockWindow, TEST_URL_VALID, EXPECTED_WINDOW_NAME, FEATURES); | ||
}); | ||
}); | ||
|
||
test('Works with valid window, url, windowName, features and setting left+right', () => { | ||
const EXPECTED_WINDOW_NAME = 'FooBar'; | ||
|
||
return new Promise((resolve) => { | ||
const screen = { width: 400, height: 300 }; | ||
const screenSetup = { width: 400, height: 300 }; | ||
const desiredFeatures = { width: 100, height: 200 }; | ||
const open = (url, windowName, features) => { | ||
expect(url).toBe('http://example.com'); | ||
expect(windowName).toBe('FooBar'); | ||
const left = (screen.width - desiredFeatures.width) / 2; | ||
const top = (screen.height - desiredFeatures.height) / 2; | ||
const open = (url: string, windowName: string, features: string) => { | ||
expect(url).toBe(TEST_URL_VALID); | ||
expect(windowName).toBe(EXPECTED_WINDOW_NAME); | ||
const left = (screenSetup.width - desiredFeatures.width) / 2; | ||
const top = (screenSetup.height - desiredFeatures.height) / 2; | ||
expect(features) | ||
.toBe(defaultFeatures + `,width=100,height=200,left=${left},top=${top}`); | ||
resolve(); | ||
resolve(''); | ||
}; | ||
const spy = jest.fn().mockImplementation(open); | ||
|
||
mockWindow.screen = { | ||
...SCREEN, | ||
...screenSetup, | ||
}; | ||
const window = { screen, open }; | ||
popup.open(window, 'http://example.com', 'FooBar', desiredFeatures); | ||
mockWindow.open = spy; | ||
popup.open(mockWindow, TEST_URL_VALID, EXPECTED_WINDOW_NAME, desiredFeatures); | ||
}); | ||
}); | ||
}); |
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