UNPKG

2.86 kBJavaScriptView Raw
1import { expect } from 'chai';
2import entries from 'object.entries';
3import CSSInterface from '../src';
4
5import registerCSSInterfaceNamespace from '../src/utils/registerCSSInterfaceNamespace';
6import getClassName from '../src/utils/getClassName';
7
8const { create, resolve } = CSSInterface;
9const testStyles = {
10 iguana: { color: 'red' },
11};
12
13describe('create', () => {
14 afterEach(() => {
15 registerCSSInterfaceNamespace('');
16 });
17
18 it('returns an object mapping style names to class names', () => {
19 const stylesToClasses = create(testStyles);
20 const { length: testStylesLength } = Object.keys(testStyles);
21
22 expect(Object.keys(stylesToClasses)).to.have.lengthOf(testStylesLength);
23
24 entries(stylesToClasses).forEach(([styleName, className]) => {
25 const expectedClassName = getClassName('', styleName);
26 expect(className).to.equal(expectedClassName);
27 });
28 });
29
30 it('uses namespace in class name', () => {
31 const namespace = 'Test';
32 registerCSSInterfaceNamespace(namespace);
33
34 const stylesToClasses = create(testStyles);
35 const { length: testStylesLength } = Object.keys(testStyles);
36
37 expect(Object.keys(stylesToClasses)).to.have.lengthOf(testStylesLength);
38
39 entries(stylesToClasses).forEach(([styleName, className]) => {
40 const expectedClassName = getClassName(namespace, styleName);
41 expect(className).to.equal(expectedClassName);
42 });
43 });
44});
45
46describe('resolve', () => {
47 it('accepts an array of class names and inline style objects', () => {
48 const result = resolve([
49 'a',
50 'b',
51 'c',
52 { color: 'orange', width: '10px' },
53 { color: 'red', height: '5px' },
54 ]);
55 expect(result).to.have.property('className');
56 expect(result).to.have.property('style');
57 });
58
59 it('accepts an array of arrays of class names and inline style objects', () => {
60 const result = resolve([
61 ['a', 'b', 'c'],
62 [{ color: 'orange', width: '10px' }, { color: 'red', height: '5px' }],
63 ]);
64 expect(result).to.have.property('className');
65 expect(result).to.have.property('style');
66 });
67
68 it('returns style and className props with position suffixes', () => {
69 const { className, style } = resolve([
70 'a',
71 'b',
72 'c',
73 { color: 'orange', width: '10px' },
74 { color: 'red', height: '5px' },
75 ]);
76 expect(className).to.equal('a a_1 b b_2 c c_3');
77 expect(style).to.deep.equal({ color: 'red', width: '10px', height: '5px' });
78 });
79});
80
81describe('registerCSSInterfaceNamespace', () => {
82 it('registers namespace with the interface', () => {
83 const namespace = 'Test';
84 registerCSSInterfaceNamespace(namespace);
85 const stylesToClasses = create(testStyles);
86 Object.keys(testStyles).forEach((styleName) => {
87 expect(stylesToClasses[styleName]).to.equal(`${namespace}__${styleName}`);
88 });
89 });
90});