Skip to content

Commit 4bd14be

Browse files
committed
Made the runtime work with an invalid config
1 parent 3c1fb57 commit 4bd14be

File tree

7 files changed

+68
-12
lines changed

7 files changed

+68
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 1.2.3 (2017-03-03)
4+
5+
- Made the runtime work with an invalid config
6+
37
## 1.2.2 (2017-03-02)
48

59
- Improved browser support:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zeecoder/container-query",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "A PostCSS plugin and Javascript runtime combination, which allows you to write @container queries in your CSS the same way you would write @media queries.",
55
"main": "index.js",
66
"author": "Viktor Hubert <rpgmorpheus@gmail.com>",

src/runtime/Container.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ test('appropriate instantiation', () => {
1414

1515
const containerInstance = new Container(containerElement, config);
1616
containerInstance.adjust();
17+
containerInstance.adjust();
18+
containerInstance.adjust();
1719

1820
expect(processConfig).toHaveBeenCalledTimes(1);
19-
expect(adjustContainer).toHaveBeenCalledTimes(2);
21+
expect(adjustContainer).toHaveBeenCalledTimes(4);
2022
expect(adjustContainer.mock.calls[0][0]).toBe(containerElement);
2123
expect(adjustContainer.mock.calls[0][1]).toBe(processedConfig);
2224
expect(adjustContainer.mock.calls[1][0]).toBe(containerElement);

src/runtime/adjustContainer.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ import applyStylesToElements from "./applyStylesToElements";
88
* provided configuration, if criterion are met.
99
*
1010
* @param {HTMLElement} container
11-
* @param {Object} config
11+
* @param {Object} [config] Expects a configuration object that was processed
12+
* (and validated) by `processConfig`
1213
*/
13-
export default function adjustContainer (container, config) {
14+
export default function adjustContainer (container, config = null) {
15+
if (config === null) {
16+
return;
17+
}
18+
1419
const containerDimensions = getContainerDimensions(container);
1520
const queriesLength = config.queries.length;
1621
const changeSets = {};

src/runtime/adjustContainer.spec.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import {
44
WIDTH_UNIT,
55
} from "../constants";
66

7+
jest.mock('./getContainerDimensions');
8+
79
const container = {
810
style: {},
9-
clientWidth: 99,
10-
clientHeight: 100,
1111
querySelectorAll: () => [ containerElement ],
1212
};
1313

@@ -76,9 +76,18 @@ const config = {
7676
],
7777
};
7878

79+
beforeEach(() => {
80+
require('./getContainerDimensions').default.mockClear();
81+
});
82+
7983
test('The container and its elements should be properly adjusted with the defaults', () => {
84+
const getContainerDimensionsMock = require('./getContainerDimensions').default.mockImplementation(() => {
85+
return { width: 99, height: 100 };
86+
});
87+
8088
adjustContainer(container, config);
8189

90+
expect(getContainerDimensionsMock).toHaveBeenCalledTimes(1);
8291
expect(container.style).toEqual({
8392
borderWidth: 'calc(10px + 9.9px)',
8493
});
@@ -92,10 +101,13 @@ test('The container and its elements should be properly adjusted with the defaul
92101

93102
describe('query styles should be applied, then removed when conditions no longer apply', () => {
94103
test('Apply query styles with width >= 100', () => {
95-
container.clientWidth = 100;
104+
const getContainerDimensionsMock = require('./getContainerDimensions').default.mockImplementation(() => {
105+
return { width: 100, height: 100 };
106+
});
96107

97108
adjustContainer(container, config);
98109

110+
expect(getContainerDimensionsMock).toHaveBeenCalledTimes(1);
99111
expect(container.style).toEqual({
100112
borderWidth: 'calc(20px + 20px)',
101113
});
@@ -108,10 +120,13 @@ describe('query styles should be applied, then removed when conditions no longer
108120
});
109121

110122
test('Apply query styles with height >= 200', () => {
111-
container.clientHeight = 200;
123+
const getContainerDimensionsMock = require('./getContainerDimensions').default.mockImplementation(() => {
124+
return { width: 100, height: 200 };
125+
});
112126

113127
adjustContainer(container, config);
114128

129+
expect(getContainerDimensionsMock).toHaveBeenCalledTimes(1);
115130
expect(container.style).toEqual({
116131
borderWidth: 'calc(40px + 20px)',
117132
});
@@ -124,11 +139,13 @@ describe('query styles should be applied, then removed when conditions no longer
124139
});
125140

126141
test('Remove all query styles, resetting back to the defaults', () => {
127-
container.clientWidth = 99;
128-
container.clientHeight = 99;
142+
const getContainerDimensionsMock = require('./getContainerDimensions').default.mockImplementation(() => {
143+
return { width: 99, height: 99 };
144+
});
129145

130146
adjustContainer(container, config);
131147

148+
expect(getContainerDimensionsMock).toHaveBeenCalledTimes(1);
132149
expect(container.style).toEqual({
133150
borderWidth: 'calc(9.9px + 9.9px)',
134151
});
@@ -140,3 +157,11 @@ describe('query styles should be applied, then removed when conditions no longer
140157
});
141158
});
142159
});
160+
161+
test("shouldn't adjust if the configuration is null (invalid)", () => {
162+
const getContainerDimensionsMock = require('./getContainerDimensions').default.mockImplementation(() => { console.log('called') });
163+
164+
adjustContainer(container);
165+
166+
expect(getContainerDimensionsMock).toHaveBeenCalledTimes(0);
167+
});

src/runtime/processConfig.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ import getConditionFunction from './getConditionFunction';
88
*
99
* @param {Object} origConfig
1010
*
11-
* @returns {Object}
11+
* @returns {Object|null} Return null for invalid configurations
1212
*/
1313
export default function processConfig (origConfig) {
14+
// Validate configuration before processing
15+
if (
16+
typeof origConfig !== 'object' ||
17+
typeof origConfig.selector !== 'string' ||
18+
!Array.isArray(origConfig.queries)
19+
) {
20+
return null;
21+
}
22+
23+
// Configuration seems valid, process it
1424
let config = objectAssign({}, origConfig);
1525

1626
config.queries.forEach((queryData) => {

src/runtime/processConfig.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ test('should turn conditions to functions', () => {
1010
.mockImplementationOnce(() => () => 3);
1111

1212
const origConfig = {
13+
selector: '.some-container',
1314
queries: [
1415
{ conditions: [ [ 'orientation', ':', 'landscape' ] ] },
1516
{ conditions: [ [ 'orientation', ':', 'portrait' ] ] },
1617
{ conditions: [ [ 'width', '>', 100 ] ] },
17-
]
18+
],
1819
};
1920

2021
const processedConfig = processConfig(origConfig);
@@ -31,3 +32,12 @@ test('should turn conditions to functions', () => {
3132
expect(typeof processedConfig.queries[2].conditionFunction).toBe('function');
3233
expect(processedConfig.queries[2].conditionFunction()).toBe(3);
3334
});
35+
36+
test('should return null for invalid configurations', () => {
37+
expect(processConfig()).toEqual(null);
38+
expect(processConfig({})).toEqual(null);
39+
expect(processConfig([])).toEqual(null);
40+
expect(processConfig('')).toEqual(null);
41+
expect(processConfig(false)).toEqual(null);
42+
expect(processConfig(42)).toEqual(null);
43+
});

0 commit comments

Comments
 (0)