|
| 1 | +/* eslint-disable max-nested-callbacks */ |
| 2 | + |
| 3 | +import { |
| 4 | + expect |
| 5 | +} from 'chai'; |
| 6 | + |
| 7 | +import React from 'react'; |
| 8 | +import TestUtils from 'react-addons-test-utils'; |
| 9 | +import jsdom from 'jsdom'; |
| 10 | +import linkClass from './../src/linkClass'; |
| 11 | + |
| 12 | +describe('linkClass', () => { |
| 13 | + context('ReactElement does not define styleName', () => { |
| 14 | + it('does not affect element properties', () => { |
| 15 | + expect(linkClass(<div></div>)).to.deep.equal(<div></div>); |
| 16 | + }); |
| 17 | + |
| 18 | + it('does not affect element properties with a single element child', () => { |
| 19 | + expect(linkClass(<div><p></p></div>)).to.deep.equal(<div><p></p></div>); |
| 20 | + }); |
| 21 | + |
| 22 | + it('does not affect element properties with a single text child', () => { |
| 23 | + expect(linkClass(<div>test</div>)).to.deep.equal(<div>test</div>); |
| 24 | + }); |
| 25 | + |
| 26 | + it('does not affect the className', () => { |
| 27 | + expect(linkClass(<div className='foo'></div>)).to.deep.equal(<div className='foo'></div>); |
| 28 | + }); |
| 29 | + |
| 30 | + xit('does not affect element with a single children when that children is contained in an array', () => { |
| 31 | + let outcome, |
| 32 | + subject; |
| 33 | + |
| 34 | + subject = React.createElement('div', null, [ |
| 35 | + React.createElement('p') |
| 36 | + ]); |
| 37 | + outcome = React.createElement('div', null, [ |
| 38 | + React.createElement('p') |
| 39 | + ]); |
| 40 | + |
| 41 | + expect(linkClass(subject)).to.deep.equal(outcome); |
| 42 | + }); |
| 43 | + |
| 44 | + xit('does not affect element with multiple children', () => { |
| 45 | + // Using array instead of object causes the following error: |
| 46 | + // Warning: Each child in an array or iterator should have a unique "key" prop. |
| 47 | + // Check the render method of _class. See https://fb.me/react-warning-keys for more information. |
| 48 | + // @see https://github.com/facebook/react/issues/4723#issuecomment-135555277 |
| 49 | + // expect(linkClass(<div><p></p><p></p></div>)).to.deep.equal(<div><p></p><p></p></div>); |
| 50 | + |
| 51 | + let outcome, |
| 52 | + subject; |
| 53 | + |
| 54 | + subject = React.createElement('div', null, [ |
| 55 | + React.createElement('p'), |
| 56 | + React.createElement('p') |
| 57 | + ]); |
| 58 | + outcome = React.createElement('div', null, [ |
| 59 | + React.createElement('p'), |
| 60 | + React.createElement('p') |
| 61 | + ]); |
| 62 | + |
| 63 | + expect(linkClass(subject)).to.deep.equal(outcome); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + context('called with null instead of ReactElement', () => { |
| 68 | + it('returns null', () => { |
| 69 | + let subject; |
| 70 | + |
| 71 | + subject = linkClass(null); |
| 72 | + |
| 73 | + expect(subject).to.equal(null); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + context('styleName matches an existing CSS module', () => { |
| 78 | + context('when a descendant element has styleName', () => { |
| 79 | + it('assigns a generated className', () => { |
| 80 | + let subject; |
| 81 | + |
| 82 | + subject = <div> |
| 83 | + <p styleName='foo'></p> |
| 84 | + </div>; |
| 85 | + |
| 86 | + subject = linkClass(subject, { |
| 87 | + foo: 'foo-1' |
| 88 | + }); |
| 89 | + |
| 90 | + expect(subject.props.children.props.className).to.equal('foo-1'); |
| 91 | + }); |
| 92 | + }); |
| 93 | + context('when multiple descendant elements have styleName', () => { |
| 94 | + it('assigns a generated className', () => { |
| 95 | + let subject; |
| 96 | + |
| 97 | + subject = <div> |
| 98 | + <p styleName='foo'></p> |
| 99 | + <p styleName='bar'></p> |
| 100 | + </div>; |
| 101 | + |
| 102 | + subject = linkClass(subject, { |
| 103 | + foo: 'foo-1', |
| 104 | + bar: 'bar-1' |
| 105 | + }); |
| 106 | + |
| 107 | + expect(subject.props.children[0].props.className).to.equal('foo-1'); |
| 108 | + expect(subject.props.children[1].props.className).to.equal('bar-1'); |
| 109 | + }); |
| 110 | + }); |
| 111 | + context('when ReactElement does not have an existing className', () => { |
| 112 | + it('uses the generated class name to set the className property', () => { |
| 113 | + let subject; |
| 114 | + |
| 115 | + subject = <div styleName='foo'></div>; |
| 116 | + |
| 117 | + subject = linkClass(subject, { |
| 118 | + foo: 'foo-1' |
| 119 | + }); |
| 120 | + |
| 121 | + expect(subject.props.className).to.deep.equal('foo-1'); |
| 122 | + }); |
| 123 | + }); |
| 124 | + context('when ReactElement has an existing className', () => { |
| 125 | + it('appends the generated class name to the className property', () => { |
| 126 | + let subject; |
| 127 | + |
| 128 | + subject = <div className='foo' styleName='bar'></div>; |
| 129 | + |
| 130 | + subject = linkClass(subject, { |
| 131 | + bar: 'bar-1' |
| 132 | + }); |
| 133 | + |
| 134 | + expect(subject.props.className).to.deep.equal('foo bar-1'); |
| 135 | + }); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + context('styleName includes multiple whitespace characters', () => { |
| 140 | + it('resolves CSS modules', () => { |
| 141 | + let subject; |
| 142 | + |
| 143 | + subject = <div> |
| 144 | + <p styleName=' foo bar '></p> |
| 145 | + </div>; |
| 146 | + |
| 147 | + subject = linkClass(subject, { |
| 148 | + foo: 'foo-1', |
| 149 | + bar: 'bar-1' |
| 150 | + }, { |
| 151 | + allowMultiple: true |
| 152 | + }); |
| 153 | + |
| 154 | + expect(subject.props.children.props.className).to.equal('foo-1 bar-1'); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('options.allowMultiple', () => { |
| 159 | + context('when multiple module names are used', () => { |
| 160 | + context('when false', () => { |
| 161 | + it('throws an error', () => { |
| 162 | + expect(() => { |
| 163 | + linkClass(<div styleName='foo bar'></div>, {}, {allowMultiple: false}); |
| 164 | + }).to.throw(Error, 'ReactElement styleName property defines multiple module names ("foo bar").'); |
| 165 | + }); |
| 166 | + }); |
| 167 | + context('when true', () => { |
| 168 | + it('appends a generated class name for every referenced CSS module', () => { |
| 169 | + let subject; |
| 170 | + |
| 171 | + subject = <div styleName='foo bar'></div>; |
| 172 | + |
| 173 | + subject = linkClass(subject, { |
| 174 | + foo: 'foo-1', |
| 175 | + bar: 'bar-1' |
| 176 | + }, {allowMultiple: true}); |
| 177 | + |
| 178 | + expect(subject.props.className).to.deep.equal('foo-1 bar-1'); |
| 179 | + }); |
| 180 | + }); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + describe('options.errorWhenNotFound', () => { |
| 185 | + context('when styleName does not match an existing CSS module', () => { |
| 186 | + context('when false', () => { |
| 187 | + it('ignores the missing CSS module', () => { |
| 188 | + let subject; |
| 189 | + |
| 190 | + subject = <div styleName='foo'></div>; |
| 191 | + |
| 192 | + subject = linkClass(subject, {}, {errorWhenNotFound: false}); |
| 193 | + |
| 194 | + expect(subject.props.className).to.be.an('undefined'); |
| 195 | + }); |
| 196 | + }); |
| 197 | + context('when is true', () => { |
| 198 | + it('throws an error', () => { |
| 199 | + expect(() => { |
| 200 | + linkClass(<div styleName='foo'></div>, {}, {errorWhenNotFound: true}); |
| 201 | + }).to.throw(Error, '"foo" CSS module is undefined.'); |
| 202 | + }); |
| 203 | + }); |
| 204 | + }); |
| 205 | + }); |
| 206 | + |
| 207 | + context('when ReactElement includes ReactComponent', () => { |
| 208 | + let Foo, |
| 209 | + nodeList; |
| 210 | + |
| 211 | + beforeEach(() => { |
| 212 | + global.document = jsdom.jsdom(` |
| 213 | + <!DOCTYPE html> |
| 214 | + <html> |
| 215 | + <head> |
| 216 | + </head> |
| 217 | + <body> |
| 218 | + </body> |
| 219 | + </html> |
| 220 | + `); |
| 221 | + |
| 222 | + global.window = document.defaultView; |
| 223 | + |
| 224 | + Foo = class extends React.Component { |
| 225 | + render () { |
| 226 | + return <div styleName='foo'>Hello</div>; |
| 227 | + } |
| 228 | + }; |
| 229 | + |
| 230 | + nodeList = TestUtils.renderIntoDocument(linkClass(<div styleName='foo'><Foo /></div>, {foo: 'foo-1'})); |
| 231 | + }); |
| 232 | + it('processes ReactElement nodes', () => { |
| 233 | + expect(nodeList.className).to.equal('foo-1'); |
| 234 | + }); |
| 235 | + it('does not process ReactComponent nodes', () => { |
| 236 | + expect(nodeList.firstChild.className).to.equal(''); |
| 237 | + }); |
| 238 | + }); |
| 239 | +}); |
0 commit comments