diff --git a/src/analyzer/properties/index.js b/src/analyzer/properties/index.js index b12b8c7..ad510f1 100644 --- a/src/analyzer/properties/index.js +++ b/src/analyzer/properties/index.js @@ -1,7 +1,7 @@ const uniquer = require('../../utils/uniquer.js') module.exports = declarations => { - const all = declarations.map(declaration => declaration.property) + const all = declarations.map(({property}) => property) const prefixed = require('./prefixed.js')(all) const browserhacks = require('./browserhacks.js')(all) diff --git a/src/analyzer/properties/prefixed.js b/src/analyzer/properties/prefixed.js index 177e302..9c4c744 100644 --- a/src/analyzer/properties/prefixed.js +++ b/src/analyzer/properties/prefixed.js @@ -3,14 +3,7 @@ const uniquer = require('../../utils/uniquer') module.exports = properties => { const all = properties.filter(isVendorPrefixed) - - const share = (() => { - if (properties.length === 0) { - return 0 - } - - return all.length / properties.length - })() + const share = properties.length === 0 ? 0 : all.length / properties.length return { total: all.length, diff --git a/test/analyzer/properties/browserhacks.js b/test/analyzer/properties/browserhacks.js index 5dec91a..4388d9d 100644 --- a/test/analyzer/properties/browserhacks.js +++ b/test/analyzer/properties/browserhacks.js @@ -13,29 +13,33 @@ test('It responds with the correct structure', t => { }) test('It recognizes browser hacks correctly', t => { - const actual = analyze(['_color']) + const actual = analyze(['_color', '*zoom']) const expected = { - total: 1, + total: 2, unique: [ + { + value: '*zoom', + count: 1 + }, + { value: '_color', count: 1 } ], - totalUnique: 1 + totalUnique: 2 } t.deepEqual(actual, expected) }) test('It does not report values that are no browser hacks', t => { + const actual = analyze(['color']) const expected = { total: 0, unique: [], totalUnique: 0 } - const actual = analyze(['color']) - t.deepEqual(actual, expected) }) diff --git a/test/analyzer/properties/index.js b/test/analyzer/properties/index.js index 378ed26..99529d9 100644 --- a/test/analyzer/properties/index.js +++ b/test/analyzer/properties/index.js @@ -1,9 +1,60 @@ const test = require('ava') -const testScope = require('../../utils/scope-tester.js') +const analyze = require('../../../src/analyzer/properties') -const SCOPE = 'properties' +test('it responds with the correct structure', t => { + const actual = analyze([]) + const expected = { + total: 0, + totalUnique: 0, + unique: [], + browserhacks: { + total: 0, + totalUnique: 0, + unique: [] + }, + prefixed: { + total: 0, + totalUnique: 0, + unique: [], + share: 0 + } + } -test(SCOPE, async t => { - const {actual, expected} = await testScope(SCOPE) - t.deepEqual(actual[SCOPE], expected) + t.deepEqual(actual, expected) +}) + +test('it counts all properties', t => { + const fixture = [ + {property: 'color', value: 'unset'}, + {property: 'border', value: 'unset'}, + {property: 'font-size', value: 'unset'} + ] + const {total: actual} = analyze(fixture) + const expected = 3 + + t.is(actual, expected) +}) + +test('it lists all unique properties with their count and sorted alphabeticlly', t => { + const fixture = [ + {property: 'color', value: 'unset'}, + {property: 'color', value: 'unset'}, + {property: 'border', value: 'unset'} + ] + const {unique: actual} = analyze(fixture) + const expected = [{value: 'border', count: 1}, {value: 'color', count: 2}] + + t.deepEqual(actual, expected) +}) + +test('it counts all unique properties', t => { + const fixture = [ + {property: 'color', value: 'unset'}, + {property: 'color', value: 'unset'}, + {property: 'border', value: 'unset'} + ] + const {totalUnique: actual} = analyze(fixture) + const expected = 2 + + t.is(actual, expected) }) diff --git a/test/analyzer/properties/input.css b/test/analyzer/properties/input.css deleted file mode 100644 index fa8e6c4..0000000 --- a/test/analyzer/properties/input.css +++ /dev/null @@ -1,33 +0,0 @@ -.selector { property: value !ie; } -.selector { property: value\9; } -.selector { opacity: .9; } - -/** - * @keyframes have declarations too - */ -@keyframes countMeToo { - 0% { - margin: 0; - } - to { - margin: auto; - } -} - -/** - * Prefixed - */ -.baz { - -webkit-border-radius: 0; - -moz-border-radius: 0; - -o-border-radius: 0; - border-radius: 0; - background-image: -webkit-gradient(linear, left top, left bottom, from(red), to(blue)); -} - -/** - * Browserhacks - */ -.h4ck { _color: blue; } -.h4ck { *zoom: 1 } -.h4ck { *zoom: 1 } \ No newline at end of file diff --git a/test/analyzer/properties/output.json b/test/analyzer/properties/output.json deleted file mode 100644 index 1e2d4e4..0000000 --- a/test/analyzer/properties/output.json +++ /dev/null @@ -1,79 +0,0 @@ -{ - "total": 13, - "unique": [ - { - "value": "*zoom", - "count": 2 - }, - { - "value": "-moz-border-radius", - "count": 1 - }, - { - "value": "-o-border-radius", - "count": 1 - }, - { - "value": "-webkit-border-radius", - "count": 1 - }, - { - "value": "_color", - "count": 1 - }, - { - "value": "background-image", - "count": 1 - }, - { - "value": "border-radius", - "count": 1 - }, - { - "value": "margin", - "count": 2 - }, - { - "value": "opacity", - "count": 1 - }, - { - "value": "property", - "count": 2 - } - ], - "totalUnique": 10, - "prefixed": { - "total": 3, - "unique": [ - { - "value": "-moz-border-radius", - "count": 1 - }, - { - "value": "-o-border-radius", - "count": 1 - }, - { - "value": "-webkit-border-radius", - "count": 1 - } - ], - "totalUnique": 3, - "share": 0.23076923076923078 - }, - "browserhacks": { - "total": 3, - "unique": [ - { - "value": "*zoom", - "count": 2 - }, - { - "value": "_color", - "count": 1 - } - ], - "totalUnique": 2 - } -} diff --git a/test/analyzer/properties/prefixed.js b/test/analyzer/properties/prefixed.js new file mode 100644 index 0000000..30b7e8e --- /dev/null +++ b/test/analyzer/properties/prefixed.js @@ -0,0 +1,55 @@ +const test = require('ava') +const analyze = require('../../../src/analyzer/properties/prefixed') + +test('it responds with the correct structure', t => { + const actual = analyze([]) + const expected = { + total: 0, + share: 0, + totalUnique: 0, + unique: [] + } + + t.deepEqual(actual, expected) +}) + +const FIXTURE = [ + 'color', + '-webkit-animation', + '-webkit-animation', // Duplicate + '-moz-appearance' +] + +test('it counts prefixed properties', t => { + const {total: actual} = analyze(FIXTURE) + + t.is(actual, 3) +}) + +test('it calculates the prefixed share', t => { + const {share: actual} = analyze(FIXTURE) + + t.is(actual, 0.75) +}) + +test('it finds the unique prefixed properties and counts and sorts them', t => { + const {unique: actual} = analyze(FIXTURE) + const expected = [ + { + value: '-moz-appearance', + count: 1 + }, + { + value: '-webkit-animation', + count: 2 + } + ] + + t.deepEqual(actual, expected) +}) + +test('it counts the total unique prefixed properties', t => { + const {totalUnique: actual} = analyze(FIXTURE) + + t.is(actual, 2) +})