Skip to content

Commit e8c3c8a

Browse files
Add unit testing for breakpoint code
1 parent aa803be commit e8c3c8a

File tree

3 files changed

+89
-17
lines changed

3 files changed

+89
-17
lines changed

src/features/runtime-checks.js

Lines changed: 1 addition & 17 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 = []
@@ -779,20 +780,3 @@ export default class RuntimeChecks extends ContentFeature {
779780
})
780781
}
781782
}
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {Sizing[]} breakpoints
3+
* @param {Sizing} screenSize
4+
* @returns { Sizing | null}
5+
*/
6+
export function findClosestBreakpoint (breakpoints, screenSize) {
7+
let closestBreakpoint = null
8+
let closestDistance = Infinity
9+
10+
for (let i = 0; i < breakpoints.length; i++) {
11+
const breakpoint = breakpoints[i]
12+
const distance = Math.sqrt(Math.pow(breakpoint.height - screenSize.height, 2) + Math.pow(breakpoint.width - screenSize.width, 2))
13+
14+
if (distance < closestDistance) {
15+
closestBreakpoint = breakpoint
16+
closestDistance = distance
17+
}
18+
}
19+
20+
return closestBreakpoint
21+
}

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)