Skip to content

Commit 1179207

Browse files
committed
Implement SSR tests for backward-compat query prop
1 parent 5fac22a commit 1179207

File tree

2 files changed

+92
-44
lines changed

2 files changed

+92
-44
lines changed

modules/Media.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Media extends React.Component {
5757
if (typeof window !== "object") {
5858
// In case we're rendering on the server, apply the default matches
5959
let matches;
60-
if (props.defaultMatches) {
60+
if (props.defaultMatches !== undefined) {
6161
matches = props.defaultMatches;
6262
} else if (props.query) {
6363
matches = true;

modules/__tests__/Media-ssr-test.js

Lines changed: 91 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,104 @@
33
import React from "react";
44
import Media from "../Media";
55

6-
import { serverRenderStrict } from './utils';
6+
import { serverRenderStrict } from "./utils";
77

88
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+
});
3052
});
3153
});
3254

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)"
3860
};
3961

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+
});
56104
});
57105
});
58106
});

0 commit comments

Comments
 (0)