|
1 | 1 | const test = require('ava') |
2 | 2 | const analyze = require('../../../src/analyzer/stylesheets/cohesion') |
3 | 3 |
|
4 | | -test('it calculates cohesion based on the ratio of total declarations and total rules', t => { |
5 | | - const {average: actual} = analyze({total: 2}, {total: 4}) |
6 | | - t.is(actual, 2) |
| 4 | +test('it calculates average cohesion based on the ratio of total declarations and total rules', t => { |
| 5 | + const actual = analyze([ |
| 6 | + { |
| 7 | + declarations: [ |
| 8 | + {property: 'a', value: 'a'}, |
| 9 | + {property: 'b', value: 'b'}, |
| 10 | + {property: 'c', value: 'c'}, |
| 11 | + {property: 'd', value: 'd'} |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + declarations: [{property: 'a', value: 'a'}, {property: 'b', value: 'b'}] |
| 16 | + } |
| 17 | + ]) |
| 18 | + t.is(actual.average, 3) |
7 | 19 | }) |
8 | 20 |
|
9 | | -test('it calculates cohesion correctly if there are no rules and/or declarations', t => { |
10 | | - const {average: actual} = analyze({total: 0}, {total: 0}) |
11 | | - t.is(actual, 0) |
| 21 | +test('it calculates average cohesion correctly if there are no rules and/or declarations', t => { |
| 22 | + const actual = analyze([]) |
| 23 | + t.is(actual.average, 0) |
| 24 | +}) |
| 25 | + |
| 26 | +test('it calculates lowest cohesion as the rule with the most declarations', t => { |
| 27 | + const ruleWithManyDeclarations = { |
| 28 | + selectors: ['test'], |
| 29 | + declarations: [ |
| 30 | + {property: 'a', value: 'a'}, |
| 31 | + {property: 'b', value: 'b'}, |
| 32 | + {property: 'c', value: 'c'}, |
| 33 | + {property: 'd', value: 'd'} |
| 34 | + ] |
| 35 | + } |
| 36 | + const fixture = [ |
| 37 | + ruleWithManyDeclarations, |
| 38 | + { |
| 39 | + declarations: [{property: 'a', value: 'a'}] |
| 40 | + } |
| 41 | + ] |
| 42 | + const actual = analyze(fixture) |
| 43 | + |
| 44 | + t.is(actual.min.count, 4) |
| 45 | + t.deepEqual(actual.min.value, ruleWithManyDeclarations) |
| 46 | +}) |
| 47 | + |
| 48 | +test('it calculates lowest cohesion correctly if there are no declarations', t => { |
| 49 | + const actualWithNoRules = analyze([]) |
| 50 | + t.is(actualWithNoRules.min.count, 0) |
| 51 | + t.is(actualWithNoRules.min.value, null) |
| 52 | + |
| 53 | + const actualWithNoDeclarations = analyze([ |
| 54 | + { |
| 55 | + selectors: ['a'], |
| 56 | + declarations: [] |
| 57 | + } |
| 58 | + ]) |
| 59 | + t.is(actualWithNoDeclarations.min.count, 0) |
| 60 | + t.is(actualWithNoDeclarations.min.value, null) |
12 | 61 | }) |
0 commit comments