|
3 | 3 | import React from "react";
|
4 | 4 | import Media from "../Media";
|
5 | 5 |
|
6 |
| -import { serverRenderStrict } from './utils'; |
| 6 | +import { serverRenderStrict } from "./utils"; |
7 | 7 |
|
8 | 8 | describe("A <Media> in server environment", () => {
|
9 |
| - const queries = { |
10 |
| - sm: "(max-width: 1000px)", |
11 |
| - lg: "(max-width: 2000px)", |
12 |
| - xl: "(max-width: 3000px)" |
13 |
| - }; |
14 |
| - |
15 |
| - describe("when no default matches prop provided", () => { |
16 |
| - it("should render its children as if all queries are matching", () => { |
17 |
| - const element = ( |
18 |
| - <Media queries={queries}> |
19 |
| - {matches => ( |
20 |
| - (matches.sm && matches.lg && matches.xl) |
21 |
| - ? <span>All matches, render!</span> |
22 |
| - : null |
23 |
| - )} |
24 |
| - </Media> |
25 |
| - ); |
26 |
| - |
27 |
| - const result = serverRenderStrict(element); |
28 |
| - |
29 |
| - expect(result).toBe("<span>All matches, render!</span>"); |
| 9 | + describe("and a single query is defined", () => { |
| 10 | + const query = "(max-width: 1000px)"; |
| 11 | + |
| 12 | + describe("when no default matches prop provided", () => { |
| 13 | + it("should render its children as if the query matches", () => { |
| 14 | + const element = ( |
| 15 | + <Media query={query}> |
| 16 | + {matches => |
| 17 | + matches === true ? <span>Matches, render!</span> : null |
| 18 | + } |
| 19 | + </Media> |
| 20 | + ); |
| 21 | + |
| 22 | + const result = serverRenderStrict(element); |
| 23 | + |
| 24 | + expect(result).toBe("<span>Matches, render!</span>"); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("when default matches prop provided", () => { |
| 29 | + it("should render its children according to the provided defaultMatches", () => { |
| 30 | + const render = matches => (matches === true ? <span>matches</span> : null); |
| 31 | + |
| 32 | + const matched = ( |
| 33 | + <Media query={query} defaultMatches={true}> |
| 34 | + {render} |
| 35 | + </Media> |
| 36 | + ); |
| 37 | + |
| 38 | + const matchedResult = serverRenderStrict(matched); |
| 39 | + |
| 40 | + expect(matchedResult).toBe("<span>matches</span>"); |
| 41 | + |
| 42 | + const notMatched = ( |
| 43 | + <Media query={query} defaultMatches={false}> |
| 44 | + {render} |
| 45 | + </Media> |
| 46 | + ); |
| 47 | + |
| 48 | + const notMatchedResult = serverRenderStrict(notMatched); |
| 49 | + |
| 50 | + expect(notMatchedResult).toBe(""); |
| 51 | + }); |
30 | 52 | });
|
31 | 53 | });
|
32 | 54 |
|
33 |
| - describe("when default matches prop provided", () => { |
34 |
| - const defaultMatches = { |
35 |
| - sm: true, |
36 |
| - lg: false, |
37 |
| - xl: false |
| 55 | + describe("and multiple queries are defined", () => { |
| 56 | + const queries = { |
| 57 | + sm: "(max-width: 1000px)", |
| 58 | + lg: "(max-width: 2000px)", |
| 59 | + xl: "(max-width: 3000px)" |
38 | 60 | };
|
39 | 61 |
|
40 |
| - it("should render its children according to the provided defaultMatches", () => { |
41 |
| - const element = ( |
42 |
| - <Media queries={queries} defaultMatches={defaultMatches}> |
43 |
| - {matches => ( |
44 |
| - <div> |
45 |
| - {matches.sm && <span>small</span>} |
46 |
| - {matches.lg && <span>large</span>} |
47 |
| - {matches.xl && <span>extra large</span>} |
48 |
| - </div> |
49 |
| - )} |
50 |
| - </Media> |
51 |
| - ); |
52 |
| - |
53 |
| - const result = serverRenderStrict(element); |
54 |
| - |
55 |
| - expect(result).toBe("<div><span>small</span></div>"); |
| 62 | + describe("when no default matches prop provided", () => { |
| 63 | + it("should render its children as if all queries are matching", () => { |
| 64 | + const element = ( |
| 65 | + <Media queries={queries}> |
| 66 | + {matches => |
| 67 | + matches.sm && matches.lg && matches.xl ? ( |
| 68 | + <span>All matches, render!</span> |
| 69 | + ) : null |
| 70 | + } |
| 71 | + </Media> |
| 72 | + ); |
| 73 | + |
| 74 | + const result = serverRenderStrict(element); |
| 75 | + |
| 76 | + expect(result).toBe("<span>All matches, render!</span>"); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe("when default matches prop provided", () => { |
| 81 | + const defaultMatches = { |
| 82 | + sm: true, |
| 83 | + lg: false, |
| 84 | + xl: false |
| 85 | + }; |
| 86 | + |
| 87 | + it("should render its children according to the provided defaultMatches", () => { |
| 88 | + const element = ( |
| 89 | + <Media queries={queries} defaultMatches={defaultMatches}> |
| 90 | + {matches => ( |
| 91 | + <div> |
| 92 | + {matches.sm && <span>small</span>} |
| 93 | + {matches.lg && <span>large</span>} |
| 94 | + {matches.xl && <span>extra large</span>} |
| 95 | + </div> |
| 96 | + )} |
| 97 | + </Media> |
| 98 | + ); |
| 99 | + |
| 100 | + const result = serverRenderStrict(element); |
| 101 | + |
| 102 | + expect(result).toBe("<div><span>small</span></div>"); |
| 103 | + }); |
56 | 104 | });
|
57 | 105 | });
|
58 | 106 | });
|
0 commit comments