Skip to content

Commit cbabe96

Browse files
committed
Improve coverage
1 parent 03ac5e3 commit cbabe96

File tree

4 files changed

+100
-46
lines changed

4 files changed

+100
-46
lines changed

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"extends": "airbnb",
33
"parser": "babel-eslint",
4+
"env": {
5+
"browser": true
6+
},
47
"rules": {
58
"react/jsx-filename-extension": 0,
69
"import/no-extraneous-dependencies": 0

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"eslint-plugin-jsx-a11y": "^5.1.1",
1616
"eslint-plugin-react": "^7.3.0",
1717
"gh-pages": "^1.0.0",
18+
"match-media-mock": "^0.1.0",
1819
"nwb": "^0.18.10",
1920
"prop-types": "^15.5.10",
2021
"react": "^15.6.1",

src/index.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ const getClassNameOrStyleProps = (classNameOrStyle, componentState) => {
2727
};
2828

2929
const inBrowser = typeof window !== 'undefined';
30-
// eslint-disable-next-line no-undef
31-
const matchMedia = inBrowser ? window.matchMedia : null;
30+
const matchMedia = inBrowser && window.matchMedia !== null;
3231

3332
class HyperResponsiveTable extends Component {
3433
static propTypes = {
@@ -70,17 +69,10 @@ class HyperResponsiveTable extends Component {
7069
}
7170

7271
componentWillUnmount() {
73-
const self = this;
74-
this.setState((state) => {
75-
if (state.mql) {
76-
state.mql.removeListener(self.handleMatch);
77-
return {
78-
...state,
79-
mql: null,
80-
};
81-
}
82-
return state;
83-
});
72+
const { mql } = this.state;
73+
if (mql) {
74+
mql.removeListener(this.handleMatch);
75+
}
8476
}
8577

8678
updateQuery = (props) => {
@@ -89,7 +81,7 @@ class HyperResponsiveTable extends Component {
8981
let narrow = false;
9082
const { breakpoint } = props;
9183
if (matchMedia) {
92-
mql = matchMedia(typeof breakpoint === 'string' ? breakpoint : `screen and (min-width: ${breakpoint}px)`);
84+
mql = window.matchMedia(typeof breakpoint === 'string' ? breakpoint : `screen and (min-width: ${breakpoint}px)`);
9385
mql.addListener(this.handleMatch);
9486
narrow = !mql.matches;
9587
}

tests/index-test.js

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
11
import expect from 'expect';
22
import React from 'react';
33
import { render, unmountComponentAtNode } from 'react-dom';
4+
import matchMediaMock from 'match-media-mock';
45

56
import Component from '../src/';
67

8+
const matchMedia = matchMediaMock.create();
9+
window.matchMedia = matchMedia;
10+
11+
const headers = {
12+
a: 'A',
13+
b: 'B',
14+
};
15+
const rows = [
16+
{
17+
a: 'A 1',
18+
b: 'B 1',
19+
},
20+
{
21+
a: 'A 2',
22+
b: 'B 2',
23+
},
24+
];
25+
const keyGetter = r => r.a;
26+
727
describe('Component', () => {
828
let node;
929

1030
beforeEach(() => {
31+
matchMedia.setConfig({ type: 'screen', width: 1200 });
1132
node = document.createElement('div');
1233
});
1334

1435
afterEach(() => {
1536
unmountComponentAtNode(node);
1637
});
1738

18-
it('wide', () => {
19-
const headers = {
20-
a: 'A',
21-
b: 'B',
22-
};
23-
const rows = [
24-
{
25-
a: 'A 1',
26-
b: 'B 1',
27-
},
28-
{
29-
a: 'A 2',
30-
b: 'B 2',
31-
},
32-
];
33-
const keyGetter = r => r.a;
39+
it('low integer breakpoint should give wide styled table', () => {
3440
const breakpoint = 300;
3541
const props = { headers, rows, keyGetter, breakpoint };
3642

@@ -42,22 +48,7 @@ describe('Component', () => {
4248
});
4349
});
4450

45-
it('narrow', () => {
46-
const headers = {
47-
a: 'A',
48-
b: 'B',
49-
};
50-
const rows = [
51-
{
52-
a: 'A 1',
53-
b: 'B 1',
54-
},
55-
{
56-
a: 'A 2',
57-
b: 'B 2',
58-
},
59-
];
60-
const keyGetter = r => r.a;
51+
it('high integer breakpoint should give narrow styled table', () => {
6152
const breakpoint = 3000;
6253
const props = { headers, rows, keyGetter, breakpoint };
6354

@@ -68,4 +59,71 @@ describe('Component', () => {
6859
expect(node.querySelectorAll('tbody').length).toEqual(2);
6960
});
7061
});
62+
63+
it('low media query breakpoint should give wide styled table', () => {
64+
const breakpoint = 'screen and (min-width: 1000px)';
65+
const props = { headers, rows, keyGetter, breakpoint };
66+
67+
render(<Component {...props} />, node, () => {
68+
expect(node.querySelectorAll('table').length).toEqual(1);
69+
expect(node.querySelectorAll('tr').length).toEqual(3);
70+
expect(node.querySelectorAll('thead').length).toEqual(1);
71+
expect(node.querySelectorAll('tbody').length).toEqual(1);
72+
});
73+
});
74+
75+
it('tableStyling function value should give dynamic class when string is returned', () => {
76+
const tableStyling = opts => (opts.narrow ? 'narrow' : 'wide');
77+
const props = { headers, rows, keyGetter, tableStyling };
78+
79+
render(<Component {...props} breakpoint={3000} />, node, () => {
80+
expect(node.querySelectorAll('table.narrow').length).toEqual(1);
81+
expect(node.querySelectorAll('table.wide').length).toEqual(0);
82+
expect(node.querySelector('table').getAttribute('style')).toEqual(null);
83+
});
84+
85+
render(<Component {...props} breakpoint={300} />, node, () => {
86+
expect(node.querySelectorAll('table.narrow').length).toEqual(0);
87+
expect(node.querySelectorAll('table.wide').length).toEqual(1);
88+
expect(node.querySelector('table').getAttribute('style')).toEqual(null);
89+
});
90+
});
91+
92+
it('tableStyling object value should give style attribute', () => {
93+
const breakpoint = 3000;
94+
const tableStyling = { color: 'red' };
95+
const props = { headers, rows, keyGetter, breakpoint, tableStyling };
96+
97+
render(<Component {...props} />, node, () => {
98+
const table = node.querySelector('table');
99+
expect(table.getAttribute('style')).toEqual('color: red;');
100+
expect(table.getAttribute('class')).toEqual(null);
101+
});
102+
});
103+
104+
it('wide to narrow change should trigger tableStyling function call', () => {
105+
const breakpoint = 1000;
106+
const tableStyling = opts => (opts.narrow ? 'narrow' : 'wide');
107+
const props = { headers, rows, keyGetter, breakpoint, tableStyling };
108+
109+
render(<Component {...props} />, node, () => {
110+
const table = node.querySelector('table');
111+
expect(table.getAttribute('class')).toEqual('wide');
112+
113+
matchMedia.setConfig({ type: 'screen', width: 900 });
114+
expect(table.getAttribute('class')).toEqual('narrow');
115+
});
116+
});
117+
118+
it('invalid tableStyling should give no class or style attribute', () => {
119+
const breakpoint = 1000;
120+
const tableStyling = 1234;
121+
const props = { headers, rows, keyGetter, breakpoint, tableStyling };
122+
123+
render(<Component {...props} />, node, () => {
124+
const table = node.querySelector('table');
125+
expect(table.getAttribute('class')).toEqual(null);
126+
expect(table.getAttribute('style')).toEqual(null);
127+
});
128+
});
71129
});

0 commit comments

Comments
 (0)