Skip to content

Commit 9ebc0d2

Browse files
Add unit testing for breakpoint code
1 parent cac4f54 commit 9ebc0d2

File tree

3 files changed

+96
-20
lines changed

3 files changed

+96
-20
lines changed

src/features/runtime-checks.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ContentFeature from '../content-feature.js'
44
import { DDGProxy, getStackTraceOrigins, getStack, matchHostname, injectGlobalStyles, createStyleElement, postDebugMessage, taintSymbol, hasTaintedMethod, taintedOrigins, getTabHostname } from '../utils.js'
55
import { defineProperty } from '../wrapper-utils.js'
66
import { wrapScriptCodeOverload } from './runtime-checks/script-overload.js'
7+
import { findClosestBreakpoint } from './runtime-checks/helpers.js'
78
import { Reflect } from '../captured-globals.js'
89

910
let stackDomains = []
@@ -724,9 +725,7 @@ export default class RuntimeChecks extends ContentFeature {
724725
}
725726

726727
/**
727-
* @typedef {object} Sizing
728-
* @property {number} height
729-
* @property {number} width
728+
* @typedef {import('./runtime-checks/helpers.js').Sizing} Sizing
730729
*/
731730

732731
/**
@@ -779,20 +778,3 @@ export default class RuntimeChecks extends ContentFeature {
779778
})
780779
}
781780
}
782-
783-
function findClosestBreakpoint (breakpoints, screenSize) {
784-
let closestBreakpoint = null
785-
let closestDistance = Infinity
786-
787-
for (let i = 0; i < breakpoints.length; i++) {
788-
const breakpoint = breakpoints[i]
789-
const distance = Math.sqrt(Math.pow(breakpoint.height - screenSize.height, 2) + Math.pow(breakpoint.width - screenSize.width, 2))
790-
791-
if (distance < closestDistance) {
792-
closestBreakpoint = breakpoint
793-
closestDistance = distance
794-
}
795-
}
796-
797-
return closestBreakpoint
798-
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @typedef {object} Sizing
3+
* @property {number} height
4+
* @property {number} width
5+
*/
6+
7+
/**
8+
* @param {Sizing[]} breakpoints
9+
* @param {Sizing} screenSize
10+
* @returns { Sizing | null}
11+
*/
12+
export function findClosestBreakpoint (breakpoints, screenSize) {
13+
let closestBreakpoint = null
14+
let closestDistance = Infinity
15+
16+
for (let i = 0; i < breakpoints.length; i++) {
17+
const breakpoint = breakpoints[i]
18+
const distance = Math.sqrt(Math.pow(breakpoint.height - screenSize.height, 2) + Math.pow(breakpoint.width - screenSize.width, 2))
19+
20+
if (distance < closestDistance) {
21+
closestBreakpoint = breakpoint
22+
closestDistance = distance
23+
}
24+
}
25+
26+
return closestBreakpoint
27+
}

unit-test/runtime-checks.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { findClosestBreakpoint } from '../src/features/runtime-checks/helpers.js'
2+
3+
describe('Runtime checks', () => {
4+
describe('findClosestBreakpoint', () => {
5+
it('no breakpoints returns null', () => {
6+
const closest = findClosestBreakpoint([], { height: 1, width: 1 })
7+
expect(closest).toBeNull()
8+
})
9+
10+
it('picks closest when exact match', () => {
11+
const closest = findClosestBreakpoint([
12+
{ height: 1, width: 1 },
13+
{ height: 1024, width: 768 }
14+
], { height: 1024, width: 768 })
15+
expect(closest).toEqual({ height: 1024, width: 768 })
16+
})
17+
18+
it('picks closest when close matches', () => {
19+
const closest = findClosestBreakpoint([
20+
{ height: 1, width: 1 },
21+
{ height: 1000, width: 700 }
22+
], { height: 1024, width: 768 })
23+
expect(closest).toEqual({ height: 1000, width: 700 })
24+
25+
const closest2 = findClosestBreakpoint([
26+
{ height: 1, width: 1 },
27+
{ height: 1000, width: 700 },
28+
{ height: 2000, width: 800 }
29+
], { height: 1024, width: 768 })
30+
expect(closest2).toEqual({ height: 1000, width: 700 })
31+
32+
// Picks the first one if there's a tie
33+
const closest3 = findClosestBreakpoint([
34+
{ height: 1, width: 1 },
35+
{ height: 1023, width: 768 },
36+
{ height: 1025, width: 768 }
37+
], { height: 1024, width: 768 })
38+
expect(closest3).toEqual({ height: 1023, width: 768 })
39+
40+
const closest4 = findClosestBreakpoint([
41+
{ height: 1, width: 1 },
42+
{ height: 1023, width: 767 },
43+
{ height: 1023, width: 769 },
44+
{ height: 1025, width: 767 },
45+
{ height: 1025, width: 769 }
46+
], { height: 1024, width: 768 })
47+
expect(closest4).toEqual({ height: 1023, width: 767 })
48+
})
49+
50+
it('picks closest when clear match', () => {
51+
const breakpoints = [
52+
{ height: 500, width: 600 },
53+
{ height: 1024, width: 768 },
54+
{ height: 2000, width: 1000 },
55+
{ height: 20000, width: 8000 }
56+
]
57+
const closest = findClosestBreakpoint(breakpoints, { height: 1024, width: 768 })
58+
expect(closest).toEqual({ height: 1024, width: 768 })
59+
60+
const closest2 = findClosestBreakpoint(breakpoints, { height: 800, width: 600 })
61+
expect(closest2).toEqual({ height: 1024, width: 768 })
62+
63+
const closest3 = findClosestBreakpoint(breakpoints, { height: 550, width: 600 })
64+
expect(closest3).toEqual({ height: 500, width: 600 })
65+
})
66+
})
67+
})

0 commit comments

Comments
 (0)