1 | var fs = require('fs')
|
2 | var test = require('tape')
|
3 | var postcss = require('postcss')
|
4 | var doiuse = require('../')
|
5 | var atImport = require('postcss-import')
|
6 | var hasKeys = require('./has-keys')
|
7 |
|
8 | test('leaves css alone by default', function (t) {
|
9 | var css, out
|
10 | css = fs.readFileSync(require.resolve('./cases/gradient.css')).toString()
|
11 | out = postcss(doiuse({
|
12 | browsers: [
|
13 | 'ie >= 7',
|
14 | 'safari >= 6',
|
15 | 'opera >= 10.1'
|
16 | ]
|
17 | })).process(css).css
|
18 | t.equal(out, css)
|
19 | t.end()
|
20 | })
|
21 |
|
22 | test('calls back for unsupported feature usages', function (t) {
|
23 | var count, css
|
24 | css = fs.readFileSync(require.resolve('./cases/gradient.css'))
|
25 | count = 0
|
26 | postcss(doiuse({
|
27 | browsers: ['ie 8'],
|
28 | onFeatureUsage: function (usageInfo) {
|
29 | count++
|
30 | hasKeys(t, usageInfo, ['feature', 'featureData', 'usage', 'message'])
|
31 | hasKeys(t, usageInfo.featureData, ['title', 'missing', 'missingData', 'caniuseData'])
|
32 | }
|
33 | }))
|
34 | .process(css).then(function () {
|
35 | t.equal(count, 4)
|
36 | t.end()
|
37 | })
|
38 | })
|
39 |
|
40 | test('ignores specified features and calls back for the others', function (t) {
|
41 | var count, css
|
42 | css = fs.readFileSync(require.resolve('./cases/gradient.css'))
|
43 | count = 0
|
44 | postcss(doiuse({
|
45 | browsers: ['ie 8'],
|
46 | ignore: [
|
47 | 'css-gradients'
|
48 | ],
|
49 | onFeatureUsage: function (usageInfo) {
|
50 | count++
|
51 | hasKeys(t, usageInfo, ['feature', 'featureData', 'usage', 'message'])
|
52 | hasKeys(t, usageInfo.featureData, ['title', 'missing', 'missingData', 'caniuseData'])
|
53 | }
|
54 | }))
|
55 | .process(css).then(function () {
|
56 | t.equal(count, 2)
|
57 | t.end()
|
58 | })
|
59 | })
|
60 |
|
61 | test('ignores specified files and calls back for others', function (t) {
|
62 | var run, ignoreCss, processCss, pcss
|
63 | ignoreCss = fs.readFileSync(require.resolve('./cases/ignore-file.css'))
|
64 | processCss = fs.readFileSync(require.resolve('./cases/gradient.css'))
|
65 | run = false
|
66 |
|
67 | pcss = function () {
|
68 | return postcss(doiuse({
|
69 | browsers: ['ie 6'],
|
70 | ignoreFiles: ['**/ignore-file.css'],
|
71 | onFeatureUsage: function (usageInfo) {
|
72 | run = true
|
73 | }
|
74 | }))
|
75 | }
|
76 |
|
77 | pcss().process(ignoreCss, {from: './cases/ignore-file.css'})
|
78 | .then(function () {
|
79 | t.false(run, 'should be false')
|
80 | })
|
81 | .then(function () {
|
82 | return pcss().process(processCss, {from: './cases/gradient.css'})
|
83 | })
|
84 | .then(function () {
|
85 | t.true(run, 'should be true')
|
86 | t.end()
|
87 | })
|
88 | })
|
89 |
|
90 | test('ignores rules from some imported files, and not others', function (t) {
|
91 | var count, css, cssPath
|
92 | cssPath = require.resolve('./cases/ignore-import.css')
|
93 | css = fs.readFileSync(cssPath)
|
94 | count = 0
|
95 |
|
96 | postcss([atImport(),
|
97 | doiuse({
|
98 | browsers: ['ie 6'],
|
99 | ignoreFiles: ['**/ignore-file.css'],
|
100 | onFeatureUsage: function (usageInfo) {
|
101 | count++
|
102 | }
|
103 | })])
|
104 | .process(css, {from: cssPath})
|
105 | .then(function () {
|
106 | t.equal(count, 2)
|
107 | t.end()
|
108 | })
|
109 | })
|
110 |
|
111 | test('ignores rules specified in comments', function (t) {
|
112 | var count, ignoreCss, ignoreCssPath, processCss, processCssPath
|
113 | ignoreCssPath = require.resolve('./cases/ignore-comment.css')
|
114 | ignoreCss = fs.readFileSync(ignoreCssPath)
|
115 |
|
116 | processCssPath = require.resolve('./cases/ignore-file.css')
|
117 | processCss = fs.readFileSync(processCssPath)
|
118 |
|
119 | count = 0
|
120 |
|
121 | var processor = postcss([atImport(),
|
122 | doiuse({
|
123 | browsers: ['ie 6'],
|
124 | onFeatureUsage: function (usageInfo) {
|
125 | count++
|
126 | }
|
127 | })])
|
128 |
|
129 | processor.process(ignoreCss, {from: ignoreCssPath})
|
130 | .then(function () {
|
131 | t.equal(count, 2)
|
132 | }).then(function () {
|
133 | processor.process(processCss, {from: processCssPath})
|
134 | .then(function () {
|
135 | t.equal(count, 3, 'inline css disabing rules must apply only to current file')
|
136 | t.end()
|
137 | })
|
138 | })
|
139 | })
|