Skip to content

Commit

Permalink
Added new test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei committed Apr 4, 2023
1 parent a9dd5f4 commit 1183911
Show file tree
Hide file tree
Showing 4 changed files with 6,974 additions and 0 deletions.
11 changes: 11 additions & 0 deletions work examples/Unit Tests/document.referrer/explanation.txt
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.
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();
});
Loading

0 comments on commit 1183911

Please sign in to comment.