Skip to content

Commit b585cbd

Browse files
committed
spec for add_to_calendar_button
1 parent 0b7ba73 commit b585cbd

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/* eslint-env jest */
2+
/**
3+
* @jest-environment jsdom
4+
*/
5+
6+
require('jest')
7+
8+
// Mock the add-to-calendar-button module
9+
jest.mock('add-to-calendar-button', () => ({}))
10+
11+
describe('add_to_calendar_button', () => {
12+
let mockJQuery
13+
14+
beforeEach(() => {
15+
// Clear the document body
16+
document.body.innerHTML = ''
17+
18+
// Mock jQuery
19+
mockJQuery = jest.fn((callback) => {
20+
if (typeof callback === 'function') {
21+
callback()
22+
}
23+
})
24+
global.$ = mockJQuery
25+
})
26+
27+
afterEach(() => {
28+
jest.resetModules()
29+
delete global.$
30+
})
31+
32+
const createCalendarButton = (dataset = {}) => {
33+
const div = document.createElement('div')
34+
div.className = 'cal-btn'
35+
Object.assign(div.dataset, {
36+
title: 'Court Hearing',
37+
start: '2025-11-15',
38+
end: '2025-11-15',
39+
tooltip: 'Add to calendar',
40+
...dataset
41+
})
42+
return div
43+
}
44+
45+
describe('createCalendarEvents', () => {
46+
test('creates add-to-calendar-button elements for all cal-btn divs', () => {
47+
// Setup
48+
const calBtn1 = createCalendarButton()
49+
const calBtn2 = createCalendarButton({
50+
title: 'Court Date 2',
51+
start: '2025-12-01',
52+
end: '2025-12-01'
53+
})
54+
document.body.appendChild(calBtn1)
55+
document.body.appendChild(calBtn2)
56+
57+
// Execute
58+
require('../src/add_to_calendar_button')
59+
60+
// Verify
61+
const buttons = document.querySelectorAll('add-to-calendar-button')
62+
expect(buttons.length).toBe(2)
63+
})
64+
65+
test('sets correct attributes on the calendar button', () => {
66+
// Setup
67+
const calBtn = createCalendarButton({
68+
title: 'Important Meeting',
69+
start: '2025-11-20',
70+
end: '2025-11-20',
71+
tooltip: 'Add this event'
72+
})
73+
document.body.appendChild(calBtn)
74+
75+
// Execute
76+
require('../src/add_to_calendar_button')
77+
78+
// Verify
79+
const button = document.querySelector('add-to-calendar-button')
80+
expect(button.getAttribute('name')).toBe('Important Meeting')
81+
expect(button.getAttribute('startDate')).toBe('2025-11-20')
82+
expect(button.getAttribute('endDate')).toBe('2025-11-20')
83+
expect(button.getAttribute('description')).toBe('Important Meeting')
84+
expect(button.getAttribute('options')).toBe("'Apple','Google','iCal','Microsoft365','Outlook.com','Yahoo'")
85+
expect(button.getAttribute('timeZone')).toBe('currentBrowser')
86+
expect(button.getAttribute('lightMode')).toBe('bodyScheme')
87+
expect(button.title).toBe('Add this event')
88+
})
89+
90+
test('clears existing content in cal-btn div', () => {
91+
// Setup
92+
const calBtn = createCalendarButton()
93+
calBtn.innerHTML = '<p>Old content</p>'
94+
document.body.appendChild(calBtn)
95+
96+
// Execute
97+
require('../src/add_to_calendar_button')
98+
99+
// Verify
100+
const oldContent = calBtn.querySelector('p')
101+
expect(oldContent).toBeNull()
102+
const button = calBtn.querySelector('add-to-calendar-button')
103+
expect(button).not.toBeNull()
104+
})
105+
106+
test('handles empty dataset gracefully', () => {
107+
// Setup
108+
const div = document.createElement('div')
109+
div.className = 'cal-btn'
110+
document.body.appendChild(div)
111+
112+
// Execute
113+
require('../src/add_to_calendar_button')
114+
115+
// Verify
116+
const button = document.querySelector('add-to-calendar-button')
117+
expect(button).not.toBeNull()
118+
expect(button.getAttribute('name')).toBe('undefined')
119+
expect(button.getAttribute('startDate')).toBe('undefined')
120+
expect(button.getAttribute('endDate')).toBe('undefined')
121+
})
122+
123+
test('does nothing when no cal-btn elements exist', () => {
124+
// Setup
125+
document.body.innerHTML = '<div class="other-content"></div>'
126+
127+
// Execute
128+
require('../src/add_to_calendar_button')
129+
130+
// Verify
131+
const buttons = document.querySelectorAll('add-to-calendar-button')
132+
expect(buttons.length).toBe(0)
133+
})
134+
135+
test('calls createCalendarEvents on DOM ready', () => {
136+
// Setup
137+
const calBtn = createCalendarButton()
138+
document.body.appendChild(calBtn)
139+
140+
// Execute
141+
require('../src/add_to_calendar_button')
142+
143+
// Verify jQuery was called
144+
expect(mockJQuery).toHaveBeenCalled()
145+
expect(mockJQuery).toHaveBeenCalledWith(expect.any(Function))
146+
147+
// Verify the calendar button was created
148+
const button = document.querySelector('add-to-calendar-button')
149+
expect(button).not.toBeNull()
150+
})
151+
152+
test('preserves cal-btn class on parent div', () => {
153+
// Setup
154+
const calBtn = createCalendarButton()
155+
document.body.appendChild(calBtn)
156+
157+
// Execute
158+
require('../src/add_to_calendar_button')
159+
160+
// Verify
161+
const divs = document.querySelectorAll('div.cal-btn')
162+
expect(divs.length).toBe(1)
163+
expect(divs[0].querySelector('add-to-calendar-button')).not.toBeNull()
164+
})
165+
})
166+
})

0 commit comments

Comments
 (0)