Skip to content
Merged
30 changes: 22 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion packages/child/methods/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'

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

expect(window.parentIFrame).toBeDefined()
})

it('size() method warns about deprecation', async () => {
const warnSpy = vi.fn()
vi.spyOn(await import('../console'), 'warn').mockImplementation(warnSpy)

setupPublicMethods()

window.parentIframe.size()

expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('renamed'))
})
})
10 changes: 10 additions & 0 deletions packages/child/page/apply-selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ describe('child/page/apply-selectors', () => {
expect(Object.hasOwn(el1.dataset, 'iframeSize')).toBe(true)
expect(Object.hasOwn(el2.dataset, 'iframeIgnore')).toBe(true)
})

it('applySelector returns early when selector is empty', () => {
vi.spyOn(childConsole, 'log').mockImplementation(() => {})
const logCallsBefore = childConsole.log.mock.calls.length

applySelector('test', 'data-test', '')

// Should not log anything beyond potentially clearing mocks
expect(childConsole.log.mock.calls.length).toBe(logCallsBefore)
})
})
11 changes: 11 additions & 0 deletions packages/child/page/reset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ describe('child/page/reset', () => {
expect(sendMessage).toHaveBeenCalledWith(10, 20, 'reset')
globalThis.requestAnimationFrame = raf
})

test('lockTrigger blocks calculation when already locked', () => {
state.triggerLocked = true

resetIframe('blocked-test')

// Should log the blocked message
expect(consoleMod.log).toHaveBeenCalledWith(
'TriggerLock blocked calculation',
)
})
})
25 changes: 25 additions & 0 deletions packages/child/read/from-page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,29 @@ describe('child/read/from-page', () => {
expect(typeof out.onReady).toBe('function')
expect(typeof out.onBeforeResize).toBe('function')
})

test('throws TypeError when value is wrong type', async () => {
window.iframeResizer = {
targetOrigin: 123, // should be string
}

const { default: readFromPage } = await import('./from-page')

expect(() => readFromPage()).toThrow(TypeError)
expect(() => readFromPage()).toThrow('targetOrigin is not a string')
})

test('reads deprecated offset option', async () => {
settings.calculateHeight = true
settings.calculateWidth = true
window.iframeResizer = {
offset: 10,
}

const { default: readFromPage } = await import('./from-page')
const out = readFromPage()

expect(out.offsetHeight).toBe(10)
expect(out.offsetWidth).toBe(10)
})
})
15 changes: 15 additions & 0 deletions packages/child/read/from-parent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,19 @@ describe('child/read/from-parent', () => {
expect(out.mode).toBe(0)
expect(out.logExpand).toBe(true)
})

test('handles undefined values gracefully', () => {
const data = []
data[0] = 'test-id'
// Leave other values undefined

const out = readFromParent(data)

expect(out.parentId).toBe('test-id')
expect(out.bodyMargin).toBeUndefined()
expect(out.calculateWidth).toBeUndefined()
expect(out.logging).toBeUndefined()
expect(out.tolerance).toBeUndefined()
expect(out.mode).toBeUndefined()
})
})
73 changes: 73 additions & 0 deletions packages/child/size/auto.branches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,77 @@ describe('child/size/auto branches', () => {
})
expect(res).toBeGreaterThanOrEqual(100)
})

it('hasTags branch uses taggedElement', async () => {
state.firstRun = false
state.hasTags = true
const res = auto({
...base,
taggedElement: () => 250,
})
expect(res).toBe(250)
})

it('html size decreased when no overflow', async () => {
// First call to establish previous size (must trigger getBoundingClientRect to set prevBoundingSize)
state.firstRun = true
state.hasOverflow = false
auto({
...base,
boundingClientRect: () => 150,
documentElementScroll: () => 100,
})

// Second call with decreased bounding size, but INCREASED scroll size
// This ensures we don't match the earlier case (line 79-81) which requires scrollSize <= prevScrollSize
state.firstRun = false
state.hasOverflow = false
const res = auto({
...base,
boundingClientRect: () => 120, // decreased from 150
documentElementScroll: () => 110, // increased from 100
})
expect(res).toBe(120)
})

it('scrollSize equals floor of boundingSize', async () => {
state.firstRun = false
const res = auto({
...base,
boundingClientRect: () => 100.7,
documentElementScroll: () => 100, // equals floor(100.7)
})
expect(res).toBe(100.7)
})

it('scrollSize equals ceil of boundingSize', async () => {
state.firstRun = false
const res = auto({
...base,
boundingClientRect: () => 100.3,
documentElementScroll: () => 101, // equals ceil(100.3)
})
expect(res).toBe(100.3)
})

it('boundingSize greater than scrollSize', async () => {
state.firstRun = false
const res = auto({
...base,
boundingClientRect: () => 180,
documentElementScroll: () => 160,
})
expect(res).toBe(180)
})

it('getOffset adds to calculated size', async () => {
state.firstRun = false
const res = auto({
...base,
boundingClientRect: () => 100,
documentElementScroll: () => 100,
getOffset: () => 15,
})
expect(res).toBe(115) // 100 + 15 offset
})
})
Loading