Skip to content

Commit 6c94637

Browse files
committed
improve js tests
1 parent dfda22f commit 6c94637

9 files changed

+136
-46
lines changed

app/javascript/__tests__/casa_case.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-env jest */
2-
import { showAlert } from '../src/casa_case'
2+
/**
3+
* @jest-environment jsdom
4+
*/
35

4-
require('jest')
6+
import { showAlert } from '../src/casa_case'
57

68
describe('showAlert', () => {
79
const defaultErrorMessageHtml = '<div>Error Message</div>'

app/javascript/__tests__/case_button_states.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/* eslint-env jest */
2+
/**
3+
* @jest-environment jsdom
4+
*/
25

36
import {
47
showBtn,
@@ -7,8 +10,6 @@ import {
710
enableBtn
811
} from '../src/casa_case'
912

10-
require('jest')
11-
1213
let button
1314

1415
beforeEach(() => {

app/javascript/__tests__/case_contact.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-env jest */
2-
import { convertDateToSystemTimeZone } from '../src/case_contact'
2+
/**
3+
* @jest-environment jsdom
4+
*/
35

4-
require('jest')
6+
import { convertDateToSystemTimeZone } from '../src/case_contact'
57

68
test('utc date is correctly converted to system date', () => {
79
expect(convertDateToSystemTimeZone('2022-06-22 17:14:50 UTC')).toEqual(new Date('2022-06-22 17:14:50 UTC'))

app/javascript/__tests__/case_emancipations.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-env jest */
2-
import { Toggler } from '../src/case_emancipation'
2+
/**
3+
* @jest-environment jsdom
4+
*/
35

4-
require('jest')
6+
import { Toggler } from '../src/case_emancipation'
57

68
let category
79
let categoryOptions

app/javascript/__tests__/notifier.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/* eslint-env jest */
2+
/**
3+
* @jest-environment jsdom
4+
*/
5+
26
import { escape } from 'lodash'
37

4-
require('jest')
58
const { Notifier, Notification } = require('../src/notifier.js')
69

710
let notificationsElement
@@ -129,6 +132,9 @@ describe('Notifier', () => {
129132

130133
$(() => {
131134
try {
135+
// Clear any existing notifications from previous tests
136+
notificationsElement.find('.messages').empty()
137+
132138
notifier.notify(notificationMessage, 'info')
133139

134140
const successMessages = notificationsElement.children('.messages').find('.success-notification')
@@ -443,7 +449,7 @@ describe('Notifier', () => {
443449
})
444450
})
445451

446-
test('throws an error and display it in a red notification when trying to stop an async operation when it\'s sexpecting to resolve none', (done) => {
452+
test('throws an error and display it in a red notification when trying to stop an async operation when it\'s expecting to resolve none', (done) => {
447453
$(() => {
448454
try {
449455
expect(() => {
@@ -459,7 +465,7 @@ describe('Notifier', () => {
459465
})
460466

461467
describe('totalNotificationCount', () => {
462-
it('returns the number of notifications the notifier currently has displayed', (done) => {
468+
test('returns the number of notifications the notifier currently has displayed', (done) => {
463469
$(() => {
464470
try {
465471
expect(notifier.totalNotificationCount()).toBe(0)

app/javascript/__tests__/password_confirmation.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-env jest */
2-
import { checkPasswordsAndDisplayPopup } from '../src/password_confirmation'
2+
/**
3+
* @jest-environment jsdom
4+
*/
35

4-
require('jest')
6+
import { checkPasswordsAndDisplayPopup } from '../src/password_confirmation'
57

68
let submitButton, passwordField, confirmationField
79

app/javascript/__tests__/require_communication_preference.test.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
/* eslint-env jest */
2-
import { displayPopUpIfPreferencesIsInvalid } from '../src/require_communication_preference'
2+
/**
3+
* @jest-environment jsdom
4+
*/
35

4-
require('jest')
6+
import { displayPopUpIfPreferencesIsInvalid } from '../src/require_communication_preference'
57

68
let saveButton
9+
let emailCheckbox
10+
let smsCheckbox
711

812
beforeEach(() => {
913
document.body.innerHTML = `
@@ -12,17 +16,19 @@ beforeEach(() => {
1216
<input class="toggle-email-notifications" type="checkbox" value="1" checked="checked" name="user[receive_email_notifications]" id="user_receive_email_notifications">
1317
<label for="user_receive_email_notifications">Email Me</label>
1418
</div>
15-
19+
1620
<div>
1721
<input class="toggle-sms-notifications" type="checkbox" value="1" name="user[receive_sms_notifications]" id="user_receive_sms_notifications">
1822
<label for="user_receive_sms_notifications">Text Me</label>
19-
23+
2024
<div class="action_container">
2125
<input type="submit" name="commit" value="Save Preferences" class="btn btn-primary mb-3 save-preference" data-disable-with="Save Preferences">
2226
</div>
2327
</form>
2428
`
2529
saveButton = document.getElementsByClassName('save-preference')[0]
30+
emailCheckbox = document.getElementById('user_receive_email_notifications')
31+
smsCheckbox = document.getElementById('user_receive_sms_notifications')
2632
})
2733

2834
describe('ensure the save button is enabled when at least one preference is selected', () => {
@@ -35,4 +41,35 @@ describe('ensure the save button is enabled when at least one preference is sele
3541
expect(isDisabled(saveButton)).toBe(false)
3642
expect(document.getElementsByClassName('swal2-container').length).toBe(0)
3743
})
44+
45+
test('at least email preference is selected', () => {
46+
emailCheckbox.checked = true
47+
smsCheckbox.checked = false
48+
displayPopUpIfPreferencesIsInvalid()
49+
expect(isDisabled(saveButton)).toBe(false)
50+
expect(document.getElementsByClassName('swal2-container').length).toBe(0)
51+
})
52+
53+
test('at least SMS preference is selected', () => {
54+
emailCheckbox.checked = false
55+
smsCheckbox.checked = true
56+
displayPopUpIfPreferencesIsInvalid()
57+
expect(isDisabled(saveButton)).toBe(false)
58+
expect(document.getElementsByClassName('swal2-container').length).toBe(0)
59+
})
60+
61+
test('both preferences are selected', () => {
62+
emailCheckbox.checked = true
63+
smsCheckbox.checked = true
64+
displayPopUpIfPreferencesIsInvalid()
65+
expect(isDisabled(saveButton)).toBe(false)
66+
expect(document.getElementsByClassName('swal2-container').length).toBe(0)
67+
})
68+
69+
test('no preferences selected shows popup', () => {
70+
emailCheckbox.checked = false
71+
smsCheckbox.checked = false
72+
displayPopUpIfPreferencesIsInvalid()
73+
expect(document.getElementsByClassName('swal2-container').length).toBe(1)
74+
})
3875
})
Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,58 @@
11
/* eslint-env jest */
2+
/**
3+
* @jest-environment jsdom
4+
*/
25

3-
require('jest')
6+
describe('session_timeout_poller', () => {
7+
let mockAlert
8+
let mockReload
9+
let originalTimeout
410

5-
describe('warningBoxAndReload', () => {
6-
test('warning box displays 2 minutes before Devise Timeout', () => {
7-
const warningBoxAndReload = jest.fn()
8-
const myTimer = jest.fn()
11+
beforeEach(() => {
12+
// Mock window.alert and window.location.reload
13+
mockAlert = jest.fn()
14+
mockReload = jest.fn()
15+
global.alert = mockAlert
16+
Object.defineProperty(window, 'location', {
17+
writable: true,
18+
value: { reload: mockReload }
19+
})
920

10-
myTimer()
11-
expect(myTimer).toHaveBeenCalled()
21+
// Set a short timeout for testing (in minutes)
22+
originalTimeout = global.window.timeout
23+
global.window.timeout = 0.05 // 3 seconds total, warning at 1.2 seconds
1224

13-
warningBoxAndReload()
14-
expect(warningBoxAndReload).toHaveBeenCalled()
25+
jest.useFakeTimers()
26+
})
27+
28+
afterEach(() => {
29+
jest.useRealTimers()
30+
global.window.timeout = originalTimeout
31+
jest.resetModules()
32+
})
33+
34+
test('timer runs every second', () => {
35+
jest.resetModules()
36+
37+
// Spy on setInterval before requiring the module
38+
const setIntervalSpy = jest.spyOn(global, 'setInterval')
39+
40+
// Start the poller
41+
require('../src/session_timeout_poller')
42+
43+
// Verify setInterval was called with 1000ms (1 second)
44+
expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), 1000)
45+
46+
setIntervalSpy.mockRestore()
47+
})
48+
49+
test('demonstrates timeout logic is testable', () => {
50+
// This test demonstrates the module loads and can be tested
51+
// A full integration test would require complex timer manipulation
52+
// due to how the module uses Date.getTime() and setInterval together
53+
expect(() => {
54+
jest.resetModules()
55+
require('../src/session_timeout_poller')
56+
}).not.toThrow()
1557
})
1658
})

app/javascript/__tests__/validated_form.test.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-env jest */
2-
require('jest')
2+
/**
3+
* @jest-environment jsdom
4+
*/
35

46
const { Notifier } = require('../src/notifier.js')
57
const { NonDrivingContactMediumWarning } = require('../src/validated_form.js')
@@ -77,32 +79,26 @@ describe('NonDrivingContactMediumWarning', () => {
7779
})
7880

7981
describe('constructor', () => {
80-
test('Throws appropriate errors when initialized with values other than a valid jQuery object', (done) => {
81-
$(() => {
82-
try {
83-
expect(() => {
84-
// eslint-disable-next-line no-new
85-
new NonDrivingContactMediumWarning(3, notifier)
86-
}).toThrow(TypeError)
87-
88-
expect(() => {
89-
// eslint-disable-next-line no-new
90-
new NonDrivingContactMediumWarning($('#non-existant'), notifier)
91-
}).toThrow(ReferenceError)
92-
93-
done()
94-
} catch (error) {
95-
done(error)
96-
}
97-
})
82+
test('Throws appropriate errors when initialized with values other than a valid jQuery object', () => {
83+
expect(() => {
84+
// eslint-disable-next-line no-new
85+
new NonDrivingContactMediumWarning(3, notifier)
86+
}).toThrow(TypeError)
87+
88+
expect(() => {
89+
// eslint-disable-next-line no-new
90+
new NonDrivingContactMediumWarning($('#non-existant'), notifier)
91+
}).toThrow(ReferenceError)
9892
})
9993
})
10094

10195
describe('getWarningState', () => {
10296
let component
10397

10498
beforeEach(() => {
105-
component = new NonDrivingContactMediumWarning($('.contact-medium.form-group input:not([type=hidden]), #case_contact_miles_driven'), notifier)
99+
$(() => {
100+
component = new NonDrivingContactMediumWarning($('.contact-medium.form-group input:not([type=hidden]), #case_contact_miles_driven'), notifier)
101+
})
106102
})
107103

108104
test('returns the warning message if a non driving contact medium is selected and the miles driven count is > 0', (done) => {

0 commit comments

Comments
 (0)