|
| 1 | +import React from "react"; |
| 2 | +import { Dimensions, Platform, Text } from "react-native"; |
| 3 | +import { render } from "@testing-library/react-native"; |
| 4 | + |
| 5 | +import MediaQuery, { isInInterval, mediaQuery } from "../MediaQuery"; |
| 6 | + |
| 7 | +const MediaQueryTestComp = ({ minHeight }: { minHeight: number }) => { |
| 8 | + return ( |
| 9 | + <MediaQuery minHeight={minHeight}> |
| 10 | + <Text>I am visible</Text> |
| 11 | + </MediaQuery> |
| 12 | + ); |
| 13 | +}; |
| 14 | + |
| 15 | +test("isInInterval", () => { |
| 16 | + expect(isInInterval(5, 1, 10)).toBeTruthy(); |
| 17 | + expect(isInInterval(11, 1, 10)).toBeFalsy(); |
| 18 | + expect(isInInterval(0, 1, 10)).toBeFalsy(); |
| 19 | +}); |
| 20 | + |
| 21 | +describe("mediaQuery", () => { |
| 22 | + test("minHeight should be false if too low", () => { |
| 23 | + const width = 100; |
| 24 | + const height = 100; |
| 25 | + const res = mediaQuery({ minHeight: 450 }, width, height); |
| 26 | + |
| 27 | + expect(res).toBeFalsy(); |
| 28 | + }); |
| 29 | + |
| 30 | + test("minHeight should be true if above", () => { |
| 31 | + const width = 100; |
| 32 | + const height = 500; |
| 33 | + const res = mediaQuery({ minHeight: 450 }, width, height); |
| 34 | + |
| 35 | + expect(res).toBeTruthy(); |
| 36 | + }); |
| 37 | + |
| 38 | + test("maxHeight should be true if lower", () => { |
| 39 | + const width = 100; |
| 40 | + const height = 100; |
| 41 | + const res = mediaQuery({ maxHeight: 450 }, width, height); |
| 42 | + |
| 43 | + expect(res).toBeTruthy(); |
| 44 | + }); |
| 45 | + |
| 46 | + test("maxHeight should be false if above", () => { |
| 47 | + const width = 100; |
| 48 | + const height = 500; |
| 49 | + const res = mediaQuery({ maxHeight: 450 }, width, height); |
| 50 | + |
| 51 | + expect(res).toBeFalsy(); |
| 52 | + }); |
| 53 | + |
| 54 | + test("minWidth should be false if too low", () => { |
| 55 | + const width = 100; |
| 56 | + const height = 100; |
| 57 | + const res = mediaQuery({ minWidth: 450 }, width, height); |
| 58 | + |
| 59 | + expect(res).toBeFalsy(); |
| 60 | + }); |
| 61 | + |
| 62 | + test("minWidth should be true if above", () => { |
| 63 | + const width = 500; |
| 64 | + const height = 100; |
| 65 | + const res = mediaQuery({ minWidth: 450 }, width, height); |
| 66 | + |
| 67 | + expect(res).toBeTruthy(); |
| 68 | + }); |
| 69 | + |
| 70 | + test("maxWidth should be true if lower", () => { |
| 71 | + const width = 100; |
| 72 | + const height = 100; |
| 73 | + const res = mediaQuery({ maxWidth: 450 }, width, height); |
| 74 | + |
| 75 | + expect(res).toBeTruthy(); |
| 76 | + }); |
| 77 | + |
| 78 | + test("maxWidth should be false if above", () => { |
| 79 | + const width = 500; |
| 80 | + const height = 100; |
| 81 | + const res = mediaQuery({ maxWidth: 450 }, width, height); |
| 82 | + |
| 83 | + expect(res).toBeFalsy(); |
| 84 | + }); |
| 85 | + |
| 86 | + test("minAspectRatio should be false if too low", () => { |
| 87 | + const width = 100; |
| 88 | + const height = 200; |
| 89 | + const res = mediaQuery({ minAspectRatio: 1 }, width, height); |
| 90 | + |
| 91 | + expect(res).toBeFalsy(); |
| 92 | + }); |
| 93 | + |
| 94 | + test("minAspectRatio should be true if above", () => { |
| 95 | + const width = 200; |
| 96 | + const height = 100; |
| 97 | + const res = mediaQuery({ minAspectRatio: 1 }, width, height); |
| 98 | + |
| 99 | + expect(res).toBeTruthy(); |
| 100 | + }); |
| 101 | + |
| 102 | + test("maxAspectRatio should be true if lower", () => { |
| 103 | + const width = 100; |
| 104 | + const height = 200; |
| 105 | + const res = mediaQuery({ maxAspectRatio: 1 }, width, height); |
| 106 | + |
| 107 | + expect(res).toBeTruthy(); |
| 108 | + }); |
| 109 | + |
| 110 | + test("maxAspectRatio should be false if above", () => { |
| 111 | + const width = 200; |
| 112 | + const height = 100; |
| 113 | + const res = mediaQuery({ maxAspectRatio: 1 }, width, height); |
| 114 | + |
| 115 | + expect(res).toBeFalsy(); |
| 116 | + }); |
| 117 | + |
| 118 | + test("minPixelRatio should be false if too low", () => { |
| 119 | + Dimensions.set({ pixelRatio: 0 }); |
| 120 | + const res = mediaQuery({ minPixelRatio: 1 }, 0, 0); |
| 121 | + |
| 122 | + expect(res).toBeFalsy(); |
| 123 | + }); |
| 124 | + |
| 125 | + test("minPixelRatio should be true if above", () => { |
| 126 | + Dimensions.set({ pixelRatio: 2 }); |
| 127 | + const res = mediaQuery({ minPixelRatio: 1 }, 0, 0); |
| 128 | + |
| 129 | + expect(res).toBeTruthy(); |
| 130 | + }); |
| 131 | + |
| 132 | + test("maxPixelRatio should be true if lower", () => { |
| 133 | + Dimensions.set({ pixelRatio: 0 }); |
| 134 | + const res = mediaQuery({ maxPixelRatio: 1 }, 0, 0); |
| 135 | + |
| 136 | + expect(res).toBeTruthy(); |
| 137 | + }); |
| 138 | + |
| 139 | + test("maxPixelRatio should be false if above", () => { |
| 140 | + Dimensions.set({ pixelRatio: 2 }); |
| 141 | + const res = mediaQuery({ maxPixelRatio: 1 }, 0, 0); |
| 142 | + |
| 143 | + expect(res).toBeFalsy(); |
| 144 | + }); |
| 145 | + |
| 146 | + test("orientation landscape should fail when expected", () => { |
| 147 | + const width = 100; |
| 148 | + const height = 200; |
| 149 | + const res = mediaQuery({ orientation: "landscape" }, width, height); |
| 150 | + |
| 151 | + expect(res).toBeFalsy(); |
| 152 | + }); |
| 153 | + |
| 154 | + test("orientation landscape should pass when expected", () => { |
| 155 | + const width = 200; |
| 156 | + const height = 100; |
| 157 | + const res = mediaQuery({ orientation: "landscape" }, width, height); |
| 158 | + |
| 159 | + expect(res).toBeTruthy(); |
| 160 | + }); |
| 161 | + |
| 162 | + test("orientation portrait should fail when expected", () => { |
| 163 | + const width = 200; |
| 164 | + const height = 100; |
| 165 | + const res = mediaQuery({ orientation: "portrait" }, width, height); |
| 166 | + |
| 167 | + expect(res).toBeFalsy(); |
| 168 | + }); |
| 169 | + |
| 170 | + test("orientation portrait should pass when expected", () => { |
| 171 | + const width = 100; |
| 172 | + const height = 200; |
| 173 | + const res = mediaQuery({ orientation: "portrait" }, width, height); |
| 174 | + |
| 175 | + expect(res).toBeTruthy(); |
| 176 | + }); |
| 177 | + |
| 178 | + test("platform should match", () => { |
| 179 | + Platform.OS = "web"; |
| 180 | + const res = mediaQuery({ platform: "web" }, 0, 0); |
| 181 | + |
| 182 | + expect(res).toBeTruthy(); |
| 183 | + }); |
| 184 | + |
| 185 | + test("platform should fail", () => { |
| 186 | + Platform.OS = "ios"; |
| 187 | + const res = mediaQuery({ platform: "web" }, 0, 0); |
| 188 | + |
| 189 | + expect(res).toBeFalsy(); |
| 190 | + }); |
| 191 | + |
| 192 | + test("condition should match", () => { |
| 193 | + const res = mediaQuery({ condition: true }, 0, 0); |
| 194 | + |
| 195 | + expect(res).toBeTruthy(); |
| 196 | + }); |
| 197 | + |
| 198 | + test("condition should fail", () => { |
| 199 | + const res = mediaQuery({ condition: false }, 0, 0); |
| 200 | + |
| 201 | + expect(res).toBeFalsy(); |
| 202 | + }); |
| 203 | +}); |
| 204 | + |
| 205 | +describe("MediaQuery Comp", () => { |
| 206 | + test("should render when constraints set", async () => { |
| 207 | + const { queryByText } = render(<MediaQueryTestComp minHeight={50} />); |
| 208 | + |
| 209 | + Dimensions.set({ height: 100, width: 100 }); |
| 210 | + |
| 211 | + expect(queryByText("I am visible")).toBeTruthy(); |
| 212 | + }); |
| 213 | + |
| 214 | + test("should not render when constraints not set", async () => { |
| 215 | + const { queryByText } = render(<MediaQueryTestComp minHeight={150} />); |
| 216 | + |
| 217 | + Dimensions.set({ height: 100, width: 100 }); |
| 218 | + |
| 219 | + expect(queryByText("I am visible")).toBeFalsy(); |
| 220 | + }); |
| 221 | + |
| 222 | + test("should re-render when constraints updated", async () => { |
| 223 | + const { queryByText } = render(<MediaQueryTestComp minHeight={150} />); |
| 224 | + |
| 225 | + Dimensions.set({ height: 100, width: 100 }); |
| 226 | + |
| 227 | + expect(queryByText("I am visible")).toBeFalsy(); |
| 228 | + |
| 229 | + Dimensions.set({ height: 200, width: 200 }); |
| 230 | + |
| 231 | + expect(queryByText("I am visible")).toBeTruthy(); |
| 232 | + }); |
| 233 | +}); |
0 commit comments