Skip to content

Commit f59bbe9

Browse files
authored
Merge pull request #19 from streamich/test/prod
docs: cosmetic improvements
2 parents dc21aaa + 815616a commit f59bbe9

15 files changed

Lines changed: 427 additions & 130 deletions

addon/__tests__/index.test.js

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/* eslint-disable */
2+
'use strict';
3+
4+
var create = require('../../index').create;
5+
6+
function findCssRuleAndDelete (selector) {
7+
var sheets = document.styleSheets;
8+
9+
for (var i = 0; i < sheets.length; i++) {
10+
var sheet = sheets[i];
11+
12+
if (!sheet.cssRules) continue;
13+
14+
for (var j = 0; j < sheet.cssRules.length; j++) {
15+
var rule = sheet.cssRules[j];
16+
17+
if (rule.selectorText === selector) {
18+
sheet.deleteRule(j);
19+
20+
return rule;
21+
}
22+
}
23+
}
24+
25+
return null;
26+
}
27+
28+
describe('nano-css', function () {
29+
it('exists', function () {
30+
expect(typeof create).toBe('function');
31+
});
32+
33+
it('can create renderer', function () {
34+
var nano = create();
35+
36+
expect(typeof nano).toBe('object');
37+
expect(typeof nano.putRaw).toBe('function');
38+
expect(typeof nano.put).toBe('function');
39+
});
40+
41+
it('default prefix is "_"', function () {
42+
expect(create().pfx).toBe('_');
43+
});
44+
45+
it('default assign function is Object.assign', function () {
46+
expect(create().assign).toBe(Object.assign);
47+
});
48+
49+
it('default stringify function is JSON.stringify', function () {
50+
expect(create().stringify).toBe(JSON.stringify);
51+
});
52+
53+
it('has no default hyperscript function', function () {
54+
expect(create().h).toBe(undefined);
55+
});
56+
57+
it('can set configuration', function () {
58+
var assign = function () {
59+
return Object.assign.apply(Object, arguments);
60+
};
61+
var h = function () {};
62+
var stringify = function () {};
63+
var nano = create({
64+
pfx: 'hello-',
65+
h: h,
66+
assign: assign,
67+
stringify: stringify
68+
});
69+
70+
expect(nano.pfx).toBe('hello-');
71+
expect(nano.h).toBe(h);
72+
expect(nano.assign).toBe(assign);
73+
expect(nano.stringify).toBe(stringify);
74+
});
75+
76+
describe('.put()', function () {
77+
it('inserts CSS', function () {
78+
var nano = create();
79+
80+
nano.put('.foo', {
81+
color: 'red'
82+
});
83+
84+
var rule = findCssRuleAndDelete('.foo');
85+
86+
expect(typeof rule).toBe('object');
87+
expect(rule.style.color).toBe('red');
88+
});
89+
90+
it('puts many declarations', function () {
91+
var nano = create();
92+
93+
nano.put('.foo2', {
94+
color: 'red',
95+
textDecoration: 'underline',
96+
'border-radius': '5px',
97+
});
98+
99+
var rule = findCssRuleAndDelete('.foo2');
100+
101+
expect(typeof rule).toBe('object');
102+
expect(rule.style.color).toBe('red');
103+
expect(rule.style['text-decoration']).toBe('underline');
104+
expect(rule.style['border-radius']).toBe('5px');
105+
});
106+
107+
it('supports class name nesting', function () {
108+
var nano = create();
109+
110+
nano.put('.foo3', {
111+
'.nested' : {
112+
color: 'blue',
113+
}
114+
});
115+
116+
var rule = findCssRuleAndDelete('.foo3 .nested');
117+
118+
expect(typeof rule).toBe('object');
119+
expect(rule.style.color).toBe('blue');
120+
});
121+
122+
it('supports pseudo selectors', function () {
123+
var nano = create();
124+
125+
nano.put('.foo3', {
126+
':hover' : {
127+
color: 'green',
128+
}
129+
});
130+
131+
var rule = findCssRuleAndDelete('.foo3:hover');
132+
133+
expect(typeof rule).toBe('object');
134+
expect(rule.style.color).toBe('green');
135+
});
136+
137+
it('can insert global styles', function () {
138+
var nano = create();
139+
140+
nano.put('', {
141+
'.global' : {
142+
color: 'green',
143+
}
144+
});
145+
146+
var rule = findCssRuleAndDelete('.global');
147+
148+
expect(typeof rule).toBe('object');
149+
expect(rule.style.color).toBe('green');
150+
});
151+
152+
it('can insert @media queries', function () {
153+
var nano = create();
154+
nano.putRaw = jest.fn();
155+
156+
nano.put('', {
157+
'@media screen': {
158+
'.global' : {
159+
color: 'green',
160+
}
161+
}
162+
});
163+
164+
165+
expect(nano.putRaw).toHaveBeenCalledTimes(1);
166+
expect(nano.putRaw.mock.calls[0][0].replace(/ /g, '')).toBe('@mediascreen{.global{color:green;}}');
167+
});
168+
});
169+
});

addon/__tests__/rule.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* eslint-disable */
2+
'use strict';
3+
4+
var create = require('../../index').create;
5+
var addonRule = require('../../addon/rule').addon;
6+
7+
function createNano (config) {
8+
var nano = create(config);
9+
10+
addonRule(nano);
11+
12+
return nano;
13+
};
14+
15+
describe('rule()', function () {
16+
it('installs rule() method', function () {
17+
var nano = create();
18+
19+
expect(typeof nano.rule).toBe('undefined');
20+
21+
addonRule(nano);
22+
23+
expect(typeof nano.rule).toBe('function');
24+
});
25+
26+
it('puts CSS styles', function () {
27+
var nano = createNano({
28+
pfx: 'test-'
29+
});
30+
31+
nano.put = jest.fn();
32+
33+
var classNames = nano.rule({
34+
color: 'red'
35+
}, 'foobar');
36+
37+
expect(nano.put).toHaveBeenCalledTimes(1);
38+
expect(nano.put).toHaveBeenCalledWith('.test-foobar', {color: 'red'});
39+
expect(classNames).toBe(' test-foobar');
40+
});
41+
42+
it('generates class name automatically if not specified', function () {
43+
var nano = createNano({
44+
pfx: 'test-'
45+
});
46+
47+
nano.put = jest.fn();
48+
49+
var css = {color: 'red'};
50+
var classNames = nano.rule(css);
51+
var computed = 'test-' + nano.hash(css);
52+
53+
expect(nano.put).toHaveBeenCalledTimes(1);
54+
expect(nano.put).toHaveBeenCalledWith('.' + computed, css);
55+
expect(classNames).toBe(' ' + computed);
56+
});
57+
});

addon/__tests__/sheet.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* eslint-disable */
2+
'use strict';
3+
4+
var create = require('../../index').create;
5+
var addonRule = require('../../addon/rule').addon;
6+
var addonSheet = require('../../addon/sheet').addon;
7+
8+
function createNano (config) {
9+
var nano = create(config);
10+
11+
addonRule(nano);
12+
addonSheet(nano);
13+
14+
return nano;
15+
};
16+
17+
describe('sheet()', function () {
18+
it('installs sheet() method', function () {
19+
var nano = create();
20+
21+
expect(typeof nano.sheet).toBe('undefined');
22+
23+
addonRule(nano);
24+
addonSheet(nano);
25+
26+
expect(typeof nano.sheet).toBe('function');
27+
});
28+
29+
it('returns a styles object', function () {
30+
var nano = createNano();
31+
var styles = nano.sheet({
32+
input: {
33+
color: 'red',
34+
},
35+
button: {
36+
color: 'blue'
37+
}
38+
});
39+
40+
expect(typeof styles.input).toBe('string');
41+
expect(typeof styles.button).toBe('string');
42+
});
43+
44+
it('inserts a rule only when first accessed', function () {
45+
var nano = createNano();
46+
47+
nano.rule = jest.fn();
48+
49+
var styles = nano.sheet({
50+
input: {
51+
color: 'red',
52+
},
53+
button: {
54+
color: 'blue'
55+
}
56+
});
57+
58+
expect(nano.rule).toHaveBeenCalledTimes(0);
59+
60+
styles.input;
61+
62+
expect(nano.rule).toHaveBeenCalledTimes(1);
63+
64+
styles.button;
65+
66+
expect(nano.rule).toHaveBeenCalledTimes(2);
67+
68+
expect(nano.rule.mock.calls[0][0]).toEqual({color: 'red'});
69+
expect(nano.rule.mock.calls[1][0]).toEqual({color: 'blue'});
70+
71+
expect(typeof nano.rule.mock.calls[0][1]).toBe('string');
72+
expect(typeof nano.rule.mock.calls[1][1]).toBe('string');
73+
});
74+
});

addon/animate/fadeIn.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
exports.addon = function (renderer) {
4+
if (process.env.NODE_ENV !== 'production') {
5+
require('../__dev__/warnOnMissingDependencies')('animate', renderer, ['keyframes']);
6+
}
7+
8+
renderer.put('', {
9+
'@keyframes fadeIn': {
10+
from: {
11+
opacity: 0,
12+
},
13+
to: {
14+
opacity: 1,
15+
}
16+
},
17+
18+
'.fadeIn': {
19+
animationName: 'fadeIn',
20+
}
21+
});
22+
};

0 commit comments

Comments
 (0)