Skip to content

Commit 50e6932

Browse files
committed
update unit tests
1 parent b7dfa72 commit 50e6932

File tree

2 files changed

+88
-24
lines changed

2 files changed

+88
-24
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Determines if a given CSS Media Query matches a set of values.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "node_modules/.bin/mocha"
7+
"test": "node_modules/.bin/mocha -R spec"
88
},
99
"repository": {
1010
"type": "git",

test/unit-tests.js

+87-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
var assert = require('chai').assert,
22
doesMQMatch = require('../');
33

4+
describe('#doesMQMatch() Type', function () {
5+
describe('Type', function(){
6+
it('should return true for a correct match', function(){
7+
assert.equal(doesMQMatch('screen and (color)', {type: 'screen', color: 1}), true);
8+
});
9+
10+
it('should return true for when type === all', function(){
11+
assert.equal(doesMQMatch('screen and (min-width: 48em)', {type: 'all', width: 1000}), true);
12+
});
13+
14+
it('should return false for an incorrect match', function(){
15+
assert.equal(doesMQMatch('screen and (min-width: 48em)', {type: 'handheld'}), false);
16+
});
17+
18+
it('should return true when no type values are passed in', function(){
19+
assert.equal(doesMQMatch('screen and (min-width: 48em)', {width: 1000}), true);
20+
});
21+
});
22+
});
423

5-
describe('#doesMQMatch()', function(){
24+
describe('#doesMQMatch() Media Features', function(){
625
describe('Width', function(){
726
it('should return true for a width higher than a min-width', function(){
827
assert.equal(doesMQMatch('(min-width: 48em)', {width: '80em'}), true);
@@ -12,8 +31,8 @@ describe('#doesMQMatch()', function(){
1231
assert.equal(doesMQMatch('(min-width: 48em)', {width: '20em'}), false);
1332
});
1433

15-
it('should return true when no width values are passed in', function(){
16-
assert.equal(doesMQMatch('(min-width: 48em)', {resolution: 72}), true);
34+
it('should return false when no width values are passed in', function(){
35+
assert.equal(doesMQMatch('(min-width: 48em)', {resolution: 72}), false);
1736
});
1837

1938
it('should return true with multiple properties', function(){
@@ -34,8 +53,8 @@ describe('#doesMQMatch()', function(){
3453
assert.equal(doesMQMatch('(min-height: 48em)', {height: '20em'}), false);
3554
});
3655

37-
it('should return true when no height values are passed in', function(){
38-
assert.equal(doesMQMatch('(min-height: 48em)', {resolution: 72}), true);
56+
it('should return false when no height values are passed in', function(){
57+
assert.equal(doesMQMatch('(min-height: 48em)', {resolution: 72}), false);
3958
});
4059

4160
it('should return true with multiple properties', function(){
@@ -57,8 +76,8 @@ describe('#doesMQMatch()', function(){
5776
assert.equal(doesMQMatch('(min-device-width: 48em)', {'device-width': '20em'}), false);
5877
});
5978

60-
it('should return true when no device-width values are passed in', function(){
61-
assert.equal(doesMQMatch('(min-device-width: 48em)', {resolution: 72}), true);
79+
it('should return false when no device-width values are passed in', function(){
80+
assert.equal(doesMQMatch('(min-device-width: 48em)', {resolution: 72}), false);
6281
});
6382

6483
it('should return true with multiple properties', function(){
@@ -79,8 +98,8 @@ describe('#doesMQMatch()', function(){
7998
assert.equal(doesMQMatch('(min-device-height: 48em)', {'device-height': '20em'}), false);
8099
});
81100

82-
it('should return true when no device-height values are passed in', function(){
83-
assert.equal(doesMQMatch('(min-device-height: 48em)', {resolution: 72}), true);
101+
it('should return false when no device-height values are passed in', function(){
102+
assert.equal(doesMQMatch('(min-device-height: 48em)', {resolution: 72}), false);
84103
});
85104

86105
it('should return true with multiple properties', function(){
@@ -103,8 +122,8 @@ describe('#doesMQMatch()', function(){
103122
assert.equal(doesMQMatch('(max-color: 2)', {color: 4 }), false);
104123
});
105124

106-
it('should return true if color isnt passed in', function(){
107-
assert.equal(doesMQMatch('max-width: 767px and (color)', {width: 300}), true);
125+
it('should return false if color isnt passed in', function(){
126+
assert.equal(doesMQMatch('max-width: 767px and (color)', {width: 300}), false);
108127
});
109128

110129
it('should work for undefined color values', function(){
@@ -122,8 +141,8 @@ describe('#doesMQMatch()', function(){
122141
assert.equal(doesMQMatch('(max-color-index: 256)', {'color-index': 512 }), false);
123142
});
124143

125-
it('should return true if color-index isnt passed in', function(){
126-
assert.equal(doesMQMatch('max-width: 767px and (color-index)', {width: 300}), true);
144+
it('should return false if color-index isnt passed in', function(){
145+
assert.equal(doesMQMatch('max-width: 767px and (color-index)', {width: 300}), false);
127146
});
128147

129148
it('should work for undefined color-index values', function(){
@@ -142,8 +161,8 @@ describe('#doesMQMatch()', function(){
142161
assert.equal(doesMQMatch('(max-monochrome: 3)', {monochrome: 4 }), false);
143162
});
144163

145-
it('should return true if monochrome isnt passed in', function(){
146-
assert.equal(doesMQMatch('max-width: 767px and (monochrome)', {width: 300}), true);
164+
it('should return false if monochrome isnt passed in', function(){
165+
assert.equal(doesMQMatch('max-width: 767px and (monochrome)', {width: 300}), false);
147166
});
148167

149168
it('should work for undefined monochrome values', function(){
@@ -162,8 +181,8 @@ describe('#doesMQMatch()', function(){
162181
assert.equal(doesMQMatch('(max-resolution: 72dpi)', {resolution: 300 }), false);
163182
});
164183

165-
it('should return true if resolution isnt passed in', function(){
166-
assert.equal(doesMQMatch('(max-resolution: 72dpi)', {width: 300}), true);
184+
it('should return false if resolution isnt passed in', function(){
185+
assert.equal(doesMQMatch('(max-resolution: 72dpi)', {width: 300}), false);
167186
});
168187

169188
it('should convert units properly', function(){
@@ -180,8 +199,8 @@ describe('#doesMQMatch()', function(){
180199
assert.equal(doesMQMatch('(max-aspect-ratio: 4/3)', {'aspect-ratio': '16/9' }), false);
181200
});
182201

183-
it('should return true if aspect-ratio isnt passed in', function(){
184-
assert.equal(doesMQMatch('(max-aspect-ratio: 72dpi)', {width: 300}), true);
202+
it('should return false if aspect-ratio isnt passed in', function(){
203+
assert.equal(doesMQMatch('(max-aspect-ratio: 72dpi)', {width: 300}), false);
185204
});
186205

187206
it('should work with strings and numbers', function(){
@@ -208,8 +227,8 @@ describe('#doesMQMatch()', function(){
208227
assert.equal(doesMQMatch('screen and (orientation: landscape)', {orientation: 'portrait' }), false);
209228
});
210229

211-
it('should return true if orientation isnt passed in', function(){
212-
assert.equal(doesMQMatch('screen and (orientation: landscape)', {width: '50em' }), true);
230+
it('should return false if orientation isnt passed in', function(){
231+
assert.equal(doesMQMatch('screen and (orientation: landscape)', {width: '50em' }), false);
213232
});
214233
});
215234

@@ -218,12 +237,57 @@ describe('#doesMQMatch()', function(){
218237
assert.equal(doesMQMatch('handheld and (grid)', {grid: true }), true);
219238
});
220239

221-
it('should return true if grid isnt passed in', function(){
222-
assert.equal(doesMQMatch('tv and (grid)', {scan: 'interlace' }), true);
240+
it('should return false if grid isnt passed in', function(){
241+
assert.equal(doesMQMatch('tv and (grid)', {scan: 'interlace' }), false);
223242
});
224243

225244
it('should return false if grid is explictly set to false', function(){
226245
assert.equal(doesMQMatch('tv and (grid)', {scan: 'interlace', grid: false }), false);
227246
});
228247
});
229248
});
249+
250+
describe('#doesMQMatch() Integration Tests', function () {
251+
describe('Real World Use Cases', function(){
252+
it('should return true because of width and type match', function(){
253+
assert.equal(doesMQMatch('screen and (min-width: 767px)', {type: 'screen', width: 980}), true);
254+
});
255+
256+
it('should return true because of width is within bounds', function(){
257+
assert.equal(doesMQMatch('screen and (min-width: 767px) and (max-width: 979px)', {type: 'screen', width: 800}), true);
258+
});
259+
260+
it('should return false because width is out of bounds', function(){
261+
assert.equal(doesMQMatch('screen and (min-width: 767px) and (max-width: 979px)', {type: 'screen', width: 980}), false);
262+
});
263+
264+
it('should return false since monochrome is not specified', function(){
265+
assert.equal(doesMQMatch('screen and (monochrome)', {width: 980}), false);
266+
});
267+
268+
it('should return true since color > 0', function(){
269+
assert.equal(doesMQMatch('screen and (color)', {type: 'all', color: 1}), true);
270+
});
271+
272+
it('should return false since color = 0', function(){
273+
assert.equal(doesMQMatch('screen and (color)', {type: 'all', color: 0}), false);
274+
});
275+
276+
});
277+
278+
describe('Grouped Media Queries', function(){
279+
it('should return true because of color', function(){
280+
assert.equal(doesMQMatch('screen and (min-width: 767px), screen and (color)', {type: 'screen', color: 1}), true);
281+
});
282+
283+
it('should return true because of width and type', function(){
284+
assert.equal(doesMQMatch('screen and (max-width: 1200px), handheld and (monochrome)', {type: 'screen', width: 1100}), true);
285+
});
286+
287+
it('should return false because of monochrome mis-match', function(){
288+
assert.equal(doesMQMatch('screen and (max-width: 1200px), handheld and (monochrome)', {type: 'screen', monochrome: 0}), false);
289+
});
290+
291+
});
292+
});
293+

0 commit comments

Comments
 (0)