UNPKG

6.61 kBJavaScriptView Raw
1import { reject, compose, toString, map, range, zipObj } from 'ramda';
2import camelcase from 'camelcase';
3import styledMQ from '../mq';
4import cssSerialiser from './helpers/cssSerialiser';
5import {
6 mqWithValidBreakpointsForRange,
7 validBreakpointsForRange,
8 mqWithNoBreakpoints,
9 invalidValues,
10 genericStrings,
11 genericValues,
12 junkValues,
13 genericNumbers,
14 genericPositiveNumbers,
15 positivePixelValues,
16 negativePixelValuesOrZero,
17 booleanValues,
18} from './data';
19import featureValues from './featureValues';
20import { rangedFeatureNames } from '../features';
21
22expect.addSnapshotSerializer(cssSerialiser);
23
24const breakpointNames = compose(map(toString), range(1))(50);
25
26const validBreakpointValuesForFeature = name => ({
27 [name]: zipObj(
28 breakpointNames,
29 featureValues(camelcase(name)).validExplicitValues
30 ),
31});
32
33describe.only('configure()', () => {
34 it("doesn't throw with default configuration", () => {
35 expect(() => mqWithNoBreakpoints().not.toThrow());
36 });
37
38 describe('breakpoints', () => {
39 it('throws if invalid breakpoint set name is supplied', () => {
40 expect(() =>
41 styledMQ.configure({ xxxx: { small: 100 } })
42 ).toThrowErrorMatchingSnapshot();
43 });
44
45 const invalidBreakpointValues = [
46 ...genericStrings,
47 ...booleanValues,
48 ...genericNumbers,
49 null,
50 NaN,
51 [],
52 ];
53
54 for (const value of invalidBreakpointValues) {
55 it(`throws if ${value} is supplied`, () => {
56 expect(() => styledMQ.configure(value)).toThrowErrorMatchingSnapshot();
57 });
58 }
59
60 it('throws if an empty object is supplied', () => {
61 expect(() => styledMQ.configure({})).toThrowErrorMatchingSnapshot();
62 });
63
64 for (const featureName of rangedFeatureNames) {
65 it("doesn't throw if breakpoint set values are valid", () => {
66 expect(() =>
67 styledMQ.configure(validBreakpointValuesForFeature(featureName))
68 ).not.toThrow();
69 });
70 }
71
72 for (const featureName of rangedFeatureNames) {
73 const invalidFeatureValues = featureValues(camelcase(featureName))
74 .invalidExplicitValues;
75
76 for (const invalidValue of invalidFeatureValues) {
77 it(`throws if invalid '${
78 featureName
79 }' breakpoint set value is supplied of '${invalidValue}'`, () => {
80 expect(() =>
81 styledMQ.configure({ [featureName]: { a: invalidValue } })
82 ).toThrowErrorMatchingSnapshot();
83 });
84 }
85 }
86 });
87
88 describe('config object', () => {
89 describe('baseFontSize', () => {
90 it("adjusts values based on 'basefontSize' for width", () => {
91 const result = mqWithValidBreakpointsForRange('width', {
92 baseFontSize: 10,
93 }).belowWidth('small');
94 expect(result).toMatchSnapshot();
95 });
96
97 it("adjusts values based on 'basefontSize' for height", () => {
98 const result = mqWithValidBreakpointsForRange('height', {
99 baseFontSize: 10,
100 }).belowHeight('small');
101 expect(result).toMatchSnapshot();
102 });
103
104 const invalidBaseFontSizes = [
105 ...invalidValues,
106 ...negativePixelValuesOrZero,
107 ];
108
109 for (const value of invalidBaseFontSizes) {
110 it(`throws if 'baseFontSize' is '${value}'`, () => {
111 expect(() =>
112 styledMQ.configure(validBreakpointsForRange('width'), {
113 baseFontSize: value,
114 })
115 ).toThrowErrorMatchingSnapshot();
116 });
117 }
118
119 const validBaseFontSizes = [
120 ...genericPositiveNumbers,
121 ...positivePixelValues,
122 ];
123
124 for (const value of validBaseFontSizes) {
125 it(`doesn't throw an error if 'baseFontSize' is '${value}'`, () => {
126 expect(() =>
127 styledMQ.configure(validBreakpointsForRange('width'), {
128 baseFontSize: value,
129 })
130 ).not.toThrow();
131 });
132 }
133 });
134
135 describe('defaultMediaType', () => {
136 const invalidDefaultMediaTypes = reject(v => v === null, genericValues);
137 for (const value of invalidDefaultMediaTypes) {
138 it(`throws if 'defaultMediaType' is '${value}'`, () => {
139 expect(() =>
140 styledMQ.configure(validBreakpointsForRange('width'), {
141 defaultMediaType: value,
142 })
143 ).toThrowErrorMatchingSnapshot();
144 });
145 }
146
147 const validDefaultMediaTypes = ['screen', 'print', 'all', 'speech', null];
148 for (const value of validDefaultMediaTypes) {
149 it(`doesn't throw an error if 'defaultMediaType' is '${value}'`, () => {
150 expect(() =>
151 styledMQ.configure(validBreakpointsForRange('width'), {
152 defaultMediaType: 'all',
153 })
154 ).not.toThrow();
155 });
156 }
157 });
158
159 describe('dimensionsUnit', () => {
160 const invalidDimensionsUnits = [...invalidValues, ...genericStrings];
161 for (const value of invalidDimensionsUnits) {
162 it(`throws if 'dimensionsUnit' is '${value}'`, () => {
163 const config = { dimensionsUnit: value };
164 expect(() =>
165 styledMQ.configure(validBreakpointsForRange('width'), config)
166 ).toThrowErrorMatchingSnapshot();
167 });
168 }
169
170 const validDimensionsUnits = ['em', 'rem', 'px'];
171 for (const value of validDimensionsUnits) {
172 it(`doesn't throw an error if 'dimensionsUnit' is '${value}'`, () => {
173 const config = { dimensionsUnit: value };
174 expect(() =>
175 styledMQ.configure(validBreakpointsForRange('width'), config)
176 ).not.toThrow();
177 });
178 }
179 });
180
181 describe('shouldSeparateQueries', () => {
182 const invalidShouldSeparateQueriesValues = [
183 ...junkValues,
184 ...genericNumbers,
185 genericStrings,
186 ];
187
188 for (const value of invalidShouldSeparateQueriesValues) {
189 it(`throws if 'shouldSeparateQueries' is '${value}'`, () => {
190 const config = { shouldSeparateQueries: value };
191 expect(() =>
192 styledMQ.configure(validBreakpointsForRange('width'), config)
193 ).toThrowErrorMatchingSnapshot();
194 });
195 }
196
197 const validShouldSeparateQueriesValues = [true, false];
198 for (const value of validShouldSeparateQueriesValues) {
199 it(`doesn't throw an error if 'shouldSeparateQueries' isn't a '${
200 value
201 }'`, () => {
202 const config = { shouldSeparateQueries: value };
203 expect(() =>
204 styledMQ.configure(validBreakpointsForRange('width'), config)
205 ).not.toThrow();
206 });
207 }
208 });
209 });
210});