Skip to content

Commit f918c42

Browse files
Merge pull request #1590 from davidjbradshaw/copilot/sub-pr-1570-another-one
Improve test coverage for core/setup, core/received, and core/page modules (96.08% stmt, 85.36% branch)
2 parents e59f2c1 + e3b441a commit f918c42

26 files changed

Lines changed: 746 additions & 9 deletions

package-lock.json

Lines changed: 22 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/child/methods/index.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it } from 'vitest'
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
22

33
import settings from '../values/settings'
44
import state from '../values/state'
@@ -29,4 +29,15 @@ describe('child/methods/index', () => {
2929

3030
expect(window.parentIFrame).toBeDefined()
3131
})
32+
33+
it('size() method warns about deprecation', async () => {
34+
const warnSpy = vi.fn()
35+
vi.spyOn(await import('../console'), 'warn').mockImplementation(warnSpy)
36+
37+
setupPublicMethods()
38+
39+
window.parentIframe.size()
40+
41+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('renamed'))
42+
})
3243
})

packages/child/page/apply-selectors.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,14 @@ describe('child/page/apply-selectors', () => {
3333
expect(Object.hasOwn(el1.dataset, 'iframeSize')).toBe(true)
3434
expect(Object.hasOwn(el2.dataset, 'iframeIgnore')).toBe(true)
3535
})
36+
37+
it('applySelector returns early when selector is empty', () => {
38+
vi.spyOn(childConsole, 'log').mockImplementation(() => {})
39+
const logCallsBefore = childConsole.log.mock.calls.length
40+
41+
applySelector('test', 'data-test', '')
42+
43+
// Should not log anything beyond potentially clearing mocks
44+
expect(childConsole.log.mock.calls.length).toBe(logCallsBefore)
45+
})
3646
})

packages/child/page/reset.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,15 @@ describe('child/page/reset', () => {
3838
expect(sendMessage).toHaveBeenCalledWith(10, 20, 'reset')
3939
globalThis.requestAnimationFrame = raf
4040
})
41+
42+
test('lockTrigger blocks calculation when already locked', () => {
43+
state.triggerLocked = true
44+
45+
resetIframe('blocked-test')
46+
47+
// Should log the blocked message
48+
expect(consoleMod.log).toHaveBeenCalledWith(
49+
'TriggerLock blocked calculation',
50+
)
51+
})
4152
})

packages/child/read/from-page.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,29 @@ describe('child/read/from-page', () => {
4747
expect(typeof out.onReady).toBe('function')
4848
expect(typeof out.onBeforeResize).toBe('function')
4949
})
50+
51+
test('throws TypeError when value is wrong type', async () => {
52+
window.iframeResizer = {
53+
targetOrigin: 123, // should be string
54+
}
55+
56+
const { default: readFromPage } = await import('./from-page')
57+
58+
expect(() => readFromPage()).toThrow(TypeError)
59+
expect(() => readFromPage()).toThrow('targetOrigin is not a string')
60+
})
61+
62+
test('reads deprecated offset option', async () => {
63+
settings.calculateHeight = true
64+
settings.calculateWidth = true
65+
window.iframeResizer = {
66+
offset: 10,
67+
}
68+
69+
const { default: readFromPage } = await import('./from-page')
70+
const out = readFromPage()
71+
72+
expect(out.offsetHeight).toBe(10)
73+
expect(out.offsetWidth).toBe(10)
74+
})
5075
})

packages/child/read/from-parent.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,19 @@ describe('child/read/from-parent', () => {
4949
expect(out.mode).toBe(0)
5050
expect(out.logExpand).toBe(true)
5151
})
52+
53+
test('handles undefined values gracefully', () => {
54+
const data = []
55+
data[0] = 'test-id'
56+
// Leave other values undefined
57+
58+
const out = readFromParent(data)
59+
60+
expect(out.parentId).toBe('test-id')
61+
expect(out.bodyMargin).toBeUndefined()
62+
expect(out.calculateWidth).toBeUndefined()
63+
expect(out.logging).toBeUndefined()
64+
expect(out.tolerance).toBeUndefined()
65+
expect(out.mode).toBeUndefined()
66+
})
5267
})

packages/child/size/auto.branches.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,77 @@ describe('child/size/auto branches', () => {
111111
})
112112
expect(res).toBeGreaterThanOrEqual(100)
113113
})
114+
115+
it('hasTags branch uses taggedElement', async () => {
116+
state.firstRun = false
117+
state.hasTags = true
118+
const res = auto({
119+
...base,
120+
taggedElement: () => 250,
121+
})
122+
expect(res).toBe(250)
123+
})
124+
125+
it('html size decreased when no overflow', async () => {
126+
// First call to establish previous size (must trigger getBoundingClientRect to set prevBoundingSize)
127+
state.firstRun = true
128+
state.hasOverflow = false
129+
auto({
130+
...base,
131+
boundingClientRect: () => 150,
132+
documentElementScroll: () => 100,
133+
})
134+
135+
// Second call with decreased bounding size, but INCREASED scroll size
136+
// This ensures we don't match the earlier case (line 79-81) which requires scrollSize <= prevScrollSize
137+
state.firstRun = false
138+
state.hasOverflow = false
139+
const res = auto({
140+
...base,
141+
boundingClientRect: () => 120, // decreased from 150
142+
documentElementScroll: () => 110, // increased from 100
143+
})
144+
expect(res).toBe(120)
145+
})
146+
147+
it('scrollSize equals floor of boundingSize', async () => {
148+
state.firstRun = false
149+
const res = auto({
150+
...base,
151+
boundingClientRect: () => 100.7,
152+
documentElementScroll: () => 100, // equals floor(100.7)
153+
})
154+
expect(res).toBe(100.7)
155+
})
156+
157+
it('scrollSize equals ceil of boundingSize', async () => {
158+
state.firstRun = false
159+
const res = auto({
160+
...base,
161+
boundingClientRect: () => 100.3,
162+
documentElementScroll: () => 101, // equals ceil(100.3)
163+
})
164+
expect(res).toBe(100.3)
165+
})
166+
167+
it('boundingSize greater than scrollSize', async () => {
168+
state.firstRun = false
169+
const res = auto({
170+
...base,
171+
boundingClientRect: () => 180,
172+
documentElementScroll: () => 160,
173+
})
174+
expect(res).toBe(180)
175+
})
176+
177+
it('getOffset adds to calculated size', async () => {
178+
state.firstRun = false
179+
const res = auto({
180+
...base,
181+
boundingClientRect: () => 100,
182+
documentElementScroll: () => 100,
183+
getOffset: () => 15,
184+
})
185+
expect(res).toBe(115) // 100 + 15 offset
186+
})
114187
})

0 commit comments

Comments
 (0)