|
| 1 | +import sinon from 'sinon'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import StronglyConnectedComponentsBuilder from '../../src/scc'; |
| 4 | +import ExportMapBuilder from '../../src/exportMap/builder'; |
| 5 | + |
| 6 | +function exportMapFixtureBuilder(path, imports) { |
| 7 | + return { |
| 8 | + path, |
| 9 | + imports: new Map(imports.map((imp) => [imp.path, { getter: () => imp }])), |
| 10 | + }; |
| 11 | +} |
| 12 | + |
| 13 | +describe('Strongly Connected Components Builder', () => { |
| 14 | + afterEach(() => ExportMapBuilder.for.restore()); |
| 15 | + afterEach(() => StronglyConnectedComponentsBuilder.clearCache()); |
| 16 | + |
| 17 | + describe('When getting an SCC', () => { |
| 18 | + const source = ''; |
| 19 | + const context = { |
| 20 | + settings: {}, |
| 21 | + parserOptions: {}, |
| 22 | + parserPath: '', |
| 23 | + }; |
| 24 | + |
| 25 | + describe('Given two files', () => { |
| 26 | + describe('When they don\'t cycle', () => { |
| 27 | + it('Should return foreign SCCs', () => { |
| 28 | + sinon.stub(ExportMapBuilder, 'for').returns( |
| 29 | + exportMapFixtureBuilder('foo.js', [exportMapFixtureBuilder('bar.js', [])]), |
| 30 | + ); |
| 31 | + const actual = StronglyConnectedComponentsBuilder.for(source, context); |
| 32 | + expect(actual).to.deep.equal({ 'foo.js': 1, 'bar.js': 0 }); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('When they do cycle', () => { |
| 37 | + it('Should return same SCC', () => { |
| 38 | + sinon.stub(ExportMapBuilder, 'for').returns( |
| 39 | + exportMapFixtureBuilder('foo.js', [ |
| 40 | + exportMapFixtureBuilder('bar.js', [ |
| 41 | + exportMapFixtureBuilder('foo.js', []), |
| 42 | + ]), |
| 43 | + ]), |
| 44 | + ); |
| 45 | + const actual = StronglyConnectedComponentsBuilder.for(source, context); |
| 46 | + expect(actual).to.deep.equal({ 'foo.js': 0, 'bar.js': 0 }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('Given three files', () => { |
| 52 | + describe('When they form a line', () => { |
| 53 | + describe('When A -> B -> C', () => { |
| 54 | + it('Should return foreign SCCs', () => { |
| 55 | + sinon.stub(ExportMapBuilder, 'for').returns( |
| 56 | + exportMapFixtureBuilder('foo.js', [ |
| 57 | + exportMapFixtureBuilder('bar.js', [ |
| 58 | + exportMapFixtureBuilder('buzz.js', []), |
| 59 | + ]), |
| 60 | + ]), |
| 61 | + ); |
| 62 | + const actual = StronglyConnectedComponentsBuilder.for(source, context); |
| 63 | + expect(actual).to.deep.equal({ 'foo.js': 2, 'bar.js': 1, 'buzz.js': 0 }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('When A -> B <-> C', () => { |
| 68 | + it('Should return 2 SCCs, A on its own', () => { |
| 69 | + sinon.stub(ExportMapBuilder, 'for').returns( |
| 70 | + exportMapFixtureBuilder('foo.js', [ |
| 71 | + exportMapFixtureBuilder('bar.js', [ |
| 72 | + exportMapFixtureBuilder('buzz.js', [ |
| 73 | + exportMapFixtureBuilder('bar.js', []), |
| 74 | + ]), |
| 75 | + ]), |
| 76 | + ]), |
| 77 | + ); |
| 78 | + const actual = StronglyConnectedComponentsBuilder.for(source, context); |
| 79 | + expect(actual).to.deep.equal({ 'foo.js': 1, 'bar.js': 0, 'buzz.js': 0 }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('When A <-> B -> C', () => { |
| 84 | + it('Should return 2 SCCs, C on its own', () => { |
| 85 | + sinon.stub(ExportMapBuilder, 'for').returns( |
| 86 | + exportMapFixtureBuilder('foo.js', [ |
| 87 | + exportMapFixtureBuilder('bar.js', [ |
| 88 | + exportMapFixtureBuilder('buzz.js', []), |
| 89 | + exportMapFixtureBuilder('foo.js', []), |
| 90 | + ]), |
| 91 | + ]), |
| 92 | + ); |
| 93 | + const actual = StronglyConnectedComponentsBuilder.for(source, context); |
| 94 | + expect(actual).to.deep.equal({ 'foo.js': 1, 'bar.js': 1, 'buzz.js': 0 }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('When A <-> B <-> C', () => { |
| 99 | + it('Should return same SCC', () => { |
| 100 | + sinon.stub(ExportMapBuilder, 'for').returns( |
| 101 | + exportMapFixtureBuilder('foo.js', [ |
| 102 | + exportMapFixtureBuilder('bar.js', [ |
| 103 | + exportMapFixtureBuilder('foo.js', []), |
| 104 | + exportMapFixtureBuilder('buzz.js', [ |
| 105 | + exportMapFixtureBuilder('bar.js', []), |
| 106 | + ]), |
| 107 | + ]), |
| 108 | + ]), |
| 109 | + ); |
| 110 | + const actual = StronglyConnectedComponentsBuilder.for(source, context); |
| 111 | + expect(actual).to.deep.equal({ 'foo.js': 0, 'bar.js': 0, 'buzz.js': 0 }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + describe('When they form a loop', () => { |
| 117 | + it('Should return same SCC', () => { |
| 118 | + sinon.stub(ExportMapBuilder, 'for').returns( |
| 119 | + exportMapFixtureBuilder('foo.js', [ |
| 120 | + exportMapFixtureBuilder('bar.js', [ |
| 121 | + exportMapFixtureBuilder('buzz.js', [ |
| 122 | + exportMapFixtureBuilder('foo.js', []), |
| 123 | + ]), |
| 124 | + ]), |
| 125 | + ]), |
| 126 | + ); |
| 127 | + const actual = StronglyConnectedComponentsBuilder.for(source, context); |
| 128 | + expect(actual).to.deep.equal({ 'foo.js': 0, 'bar.js': 0, 'buzz.js': 0 }); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('When they form a Y', () => { |
| 133 | + it('Should return 3 distinct SCCs', () => { |
| 134 | + sinon.stub(ExportMapBuilder, 'for').returns( |
| 135 | + exportMapFixtureBuilder('foo.js', [ |
| 136 | + exportMapFixtureBuilder('bar.js', []), |
| 137 | + exportMapFixtureBuilder('buzz.js', []), |
| 138 | + ]), |
| 139 | + ); |
| 140 | + const actual = StronglyConnectedComponentsBuilder.for(source, context); |
| 141 | + expect(actual).to.deep.equal({ 'foo.js': 2, 'bar.js': 0, 'buzz.js': 1 }); |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + describe('When they form a Mercedes', () => { |
| 146 | + it('Should return 1 SCC', () => { |
| 147 | + sinon.stub(ExportMapBuilder, 'for').returns( |
| 148 | + exportMapFixtureBuilder('foo.js', [ |
| 149 | + exportMapFixtureBuilder('bar.js', [ |
| 150 | + exportMapFixtureBuilder('foo.js', []), |
| 151 | + exportMapFixtureBuilder('buzz.js', []), |
| 152 | + ]), |
| 153 | + exportMapFixtureBuilder('buzz.js', [ |
| 154 | + exportMapFixtureBuilder('foo.js', []), |
| 155 | + exportMapFixtureBuilder('bar.js', []), |
| 156 | + ]), |
| 157 | + ]), |
| 158 | + ); |
| 159 | + const actual = StronglyConnectedComponentsBuilder.for(source, context); |
| 160 | + expect(actual).to.deep.equal({ 'foo.js': 0, 'bar.js': 0, 'buzz.js': 0 }); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | +}); |
0 commit comments