|
5 | 5 |
|
6 | 6 | import fs from 'fs-extra'; |
7 | 7 | import { dirname, join } from 'path'; |
8 | | -import { simpleResolver } from './utils'; |
| 8 | +import { parseCircular, simpleResolver } from './utils'; |
| 9 | +import type { Dependency } from './types'; |
| 10 | +import { DependencyKind } from './consts'; |
9 | 11 |
|
10 | 12 | describe('util', () => { |
11 | 13 | it('should resolve correctly', async () => { |
@@ -57,4 +59,204 @@ describe('util', () => { |
57 | 59 | null, |
58 | 60 | ]); |
59 | 61 | }); |
| 62 | + |
| 63 | + describe('When parsing circular', () => { |
| 64 | + function dependencyFactory(id: string): Dependency { |
| 65 | + return { |
| 66 | + issuer: '', |
| 67 | + request: '', |
| 68 | + kind: DependencyKind.StaticImport, |
| 69 | + id, |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + describe('When tree is empty', () => { |
| 74 | + const tree = {}; |
| 75 | + it('Should return empty array', () => { |
| 76 | + const actual = parseCircular(tree); |
| 77 | + expect(actual.length).toBe(0); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('When tree has just a root with no dependencies', () => { |
| 82 | + const tree = { a: [] }; |
| 83 | + it('Should return empty array', () => { |
| 84 | + const actual = parseCircular(tree); |
| 85 | + expect(actual.length).toBe(0); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('When tree has 2 nodes, no cycle', () => { |
| 90 | + const tree = { a: [dependencyFactory('b')], b: [] }; |
| 91 | + it('Should return empty array', () => { |
| 92 | + const actual = parseCircular(tree); |
| 93 | + expect(actual.length).toBe(0); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('When tree has 2 nodes, with cycle', () => { |
| 98 | + const id1 = 'a'; |
| 99 | + const id2 = 'b'; |
| 100 | + const tree = { |
| 101 | + [id1]: [dependencyFactory(id2)], |
| 102 | + [id2]: [dependencyFactory(id1)], |
| 103 | + }; |
| 104 | + |
| 105 | + let actual: Array<string[]>; |
| 106 | + beforeAll(() => { |
| 107 | + actual = parseCircular(tree); |
| 108 | + }); |
| 109 | + |
| 110 | + it('Should return non-empty array', () => { |
| 111 | + expect(actual.length).toBeGreaterThan(0); |
| 112 | + }); |
| 113 | + |
| 114 | + it('Should count only one cycle', () => { |
| 115 | + expect(actual.length).toBe(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it('Should include the ids involved in the cycle', () => { |
| 119 | + expect(actual[0]).toMatchObject([id1, id2]); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('When tree has a deep cycle', () => { |
| 124 | + const ids = ['a', 'b', 'c']; |
| 125 | + const tree = { |
| 126 | + [ids[0]]: [dependencyFactory(ids[1])], |
| 127 | + [ids[1]]: [dependencyFactory(ids[2])], |
| 128 | + [ids[2]]: [dependencyFactory(ids[0])], |
| 129 | + }; |
| 130 | + |
| 131 | + let actual: Array<string[]>; |
| 132 | + beforeAll(() => { |
| 133 | + actual = parseCircular(tree); |
| 134 | + }); |
| 135 | + |
| 136 | + it('Should return non-empty array', () => { |
| 137 | + expect(actual.length).toBeGreaterThan(0); |
| 138 | + }); |
| 139 | + |
| 140 | + it('Should count only one cycle', () => { |
| 141 | + expect(actual.length).toBe(1); |
| 142 | + }); |
| 143 | + |
| 144 | + it('Should include the ids involved in the cycle', () => { |
| 145 | + expect(actual[0]).toMatchObject(ids); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('When tree has 2 cycles with no intersection', () => { |
| 150 | + const tree = { |
| 151 | + left1: [dependencyFactory('left2')], |
| 152 | + left2: [dependencyFactory('left1')], |
| 153 | + right1: [dependencyFactory('right2')], |
| 154 | + right2: [dependencyFactory('right1')], |
| 155 | + }; |
| 156 | + |
| 157 | + let actual: Array<string[]>; |
| 158 | + beforeAll(() => { |
| 159 | + actual = parseCircular(tree); |
| 160 | + }); |
| 161 | + |
| 162 | + it('Should return non-empty array', () => { |
| 163 | + expect(actual.length).toBeGreaterThan(0); |
| 164 | + }); |
| 165 | + |
| 166 | + it('Should count two cycles', () => { |
| 167 | + expect(actual.length).toBe(2); |
| 168 | + }); |
| 169 | + |
| 170 | + it('Should include the ids involved in the cycle', () => { |
| 171 | + expect(actual[0]).toMatchObject(['left1', 'left2']); |
| 172 | + expect(actual[1]).toMatchObject(['right1', 'right2']); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('When tree has 2 cycles from common node', () => { |
| 177 | + const tree = { |
| 178 | + start: [dependencyFactory('left'), dependencyFactory('right')], |
| 179 | + left: [dependencyFactory('start')], |
| 180 | + right: [dependencyFactory('start')], |
| 181 | + }; |
| 182 | + |
| 183 | + let actual: Array<string[]>; |
| 184 | + beforeAll(() => { |
| 185 | + actual = parseCircular(tree); |
| 186 | + }); |
| 187 | + |
| 188 | + it('Should return non-empty array', () => { |
| 189 | + expect(actual.length).toBeGreaterThan(0); |
| 190 | + }); |
| 191 | + |
| 192 | + it('Should count two cycles', () => { |
| 193 | + expect(actual.length).toBe(2); |
| 194 | + }); |
| 195 | + |
| 196 | + it('Should include the ids involved in the cycle', () => { |
| 197 | + expect(actual[0]).toMatchObject(['start', 'left']); |
| 198 | + expect(actual[1]).toMatchObject(['start', 'right']); |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + describe('When tree has 2 cycles with multi-node intersection', () => { |
| 203 | + const tree = { |
| 204 | + start: [dependencyFactory('mid')], |
| 205 | + mid: [dependencyFactory('left'), dependencyFactory('right')], |
| 206 | + left: [dependencyFactory('start')], |
| 207 | + right: [dependencyFactory('start')], |
| 208 | + }; |
| 209 | + |
| 210 | + let actual: Array<string[]>; |
| 211 | + beforeAll(() => { |
| 212 | + actual = parseCircular(tree); |
| 213 | + }); |
| 214 | + |
| 215 | + it('Should return non-empty array', () => { |
| 216 | + expect(actual.length).toBeGreaterThan(0); |
| 217 | + }); |
| 218 | + |
| 219 | + it('Should count two cycles', () => { |
| 220 | + expect(actual.length).toBe(2); |
| 221 | + }); |
| 222 | + |
| 223 | + it('Should include the ids involved in the cycle', () => { |
| 224 | + expect(actual[0]).toMatchObject(['start', 'mid', 'left']); |
| 225 | + expect(actual[1]).toMatchObject(['start', 'mid', 'right']); |
| 226 | + }); |
| 227 | + }); |
| 228 | + |
| 229 | + describe('When tree has 2 cycles with intersection, different lengths', () => { |
| 230 | + const tree = { |
| 231 | + start: [dependencyFactory('left1'), dependencyFactory('right1')], |
| 232 | + left1: [dependencyFactory('left2')], |
| 233 | + left2: [dependencyFactory('intersection')], |
| 234 | + right1: [dependencyFactory('intersection')], |
| 235 | + intersection: [dependencyFactory('start')], |
| 236 | + }; |
| 237 | + |
| 238 | + let actual: Array<string[]>; |
| 239 | + beforeAll(() => { |
| 240 | + actual = parseCircular(tree); |
| 241 | + }); |
| 242 | + |
| 243 | + it('Should return non-empty array', () => { |
| 244 | + expect(actual.length).toBeGreaterThan(0); |
| 245 | + }); |
| 246 | + |
| 247 | + it('Should count two cycles', () => { |
| 248 | + expect(actual.length).toBe(2); |
| 249 | + }); |
| 250 | + |
| 251 | + it('Should include the ids involved in the cycle', () => { |
| 252 | + expect(actual[0]).toMatchObject([ |
| 253 | + 'start', |
| 254 | + 'left1', |
| 255 | + 'left2', |
| 256 | + 'intersection', |
| 257 | + ]); |
| 258 | + expect(actual[1]).toMatchObject(['start', 'right1', 'intersection']); |
| 259 | + }); |
| 260 | + }); |
| 261 | + }); |
60 | 262 | }); |
0 commit comments