UNPKG

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