-
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.
- Loading branch information
andrei
committed
Apr 4, 2023
1 parent
71f5373
commit d6282fb
Showing
4 changed files
with
6,974 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
work examples/Unit Tests/document.referrer/explanation.txt
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,11 @@ | ||
Explanation: | ||
|
||
This Jest test checks the two branches of the if-else statement in the original code. | ||
|
||
We first set the document.referrer property to a default value using Object.defineProperty. Then, we create spies for window.location.assign and window.location.reload, and mock their implementations to do nothing using jest.spyOn. | ||
|
||
Next, we run the if-else statement in the original code, and check whether window.location.assign and window.location.reload were called correctly based on the value of document.referrer. | ||
|
||
Finally, we restore the original implementations of window.location.assign and window.location.reload using spy.mockRestore() to ensure that subsequent tests are not affected. | ||
|
||
Note that we use jest.spyOn to create spies for the window.location.assign and window.location.reload methods, and use expect().toHaveBeenCalledWith() and expect().toHaveBeenCalled() to check if they were called with the expected arguments. Also, we use Object.defineProperty to set the document.referrer property. |
26 changes: 26 additions & 0 deletions
26
work examples/Unit Tests/document.referrer/validateReferrer.test.js
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,26 @@ | ||
const { JSDOM } = require('jsdom'); | ||
|
||
test('window location redirects or reloads correctly', () => { | ||
const referrer = 'http://example.com/page1.html'; | ||
Object.defineProperty(document, 'referrer', { value: referrer }); | ||
|
||
const assignSpy = jest.spyOn(window.location, 'assign').mockImplementation(() => {}); | ||
const reloadSpy = jest.spyOn(window.location, 'reload').mockImplementation(() => {}); | ||
|
||
if (document.referrer.indexOf('B2C_1A_DIA_RealMe') > -1) { | ||
window.location.href = document.referrer; | ||
} else { | ||
window.location.reload(); | ||
} | ||
|
||
if (document.referrer.indexOf('B2C_1A_DIA_RealMe') > -1) { | ||
expect(assignSpy).toHaveBeenCalledWith(referrer); | ||
expect(reloadSpy).not.toHaveBeenCalled(); | ||
} else { | ||
expect(assignSpy).not.toHaveBeenCalled(); | ||
expect(reloadSpy).toHaveBeenCalled(); | ||
} | ||
|
||
assignSpy.mockRestore(); | ||
reloadSpy.mockRestore(); | ||
}); |
Oops, something went wrong.