|
| 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