1
1
import expect from 'expect' ;
2
2
import React from 'react' ;
3
3
import { render , unmountComponentAtNode } from 'react-dom' ;
4
+ import matchMediaMock from 'match-media-mock' ;
4
5
5
6
import Component from '../src/' ;
6
7
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
+
7
27
describe ( 'Component' , ( ) => {
8
28
let node ;
9
29
10
30
beforeEach ( ( ) => {
31
+ matchMedia . setConfig ( { type : 'screen' , width : 1200 } ) ;
11
32
node = document . createElement ( 'div' ) ;
12
33
} ) ;
13
34
14
35
afterEach ( ( ) => {
15
36
unmountComponentAtNode ( node ) ;
16
37
} ) ;
17
38
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' , ( ) => {
34
40
const breakpoint = 300 ;
35
41
const props = { headers, rows, keyGetter, breakpoint } ;
36
42
@@ -42,22 +48,7 @@ describe('Component', () => {
42
48
} ) ;
43
49
} ) ;
44
50
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' , ( ) => {
61
52
const breakpoint = 3000 ;
62
53
const props = { headers, rows, keyGetter, breakpoint } ;
63
54
@@ -68,4 +59,71 @@ describe('Component', () => {
68
59
expect ( node . querySelectorAll ( 'tbody' ) . length ) . toEqual ( 2 ) ;
69
60
} ) ;
70
61
} ) ;
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
+ } ) ;
71
129
} ) ;
0 commit comments