|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { UnitTestTree } from '../../testing'; |
| 9 | +import { HostTree } from './host-tree'; |
| 10 | +import { ScopedTree } from './scoped'; |
| 11 | + |
| 12 | + |
| 13 | +describe('ScopedTree', () => { |
| 14 | + let base: HostTree; |
| 15 | + let scoped: ScopedTree; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + base = new HostTree(); |
| 19 | + base.create('/file-0-1', '0-1'); |
| 20 | + base.create('/file-0-2', '0-2'); |
| 21 | + base.create('/file-0-3', '0-3'); |
| 22 | + base.create('/level-1/file-1-1', '1-1'); |
| 23 | + base.create('/level-1/file-1-2', '1-2'); |
| 24 | + base.create('/level-1/file-1-3', '1-3'); |
| 25 | + base.create('/level-1/level-2/file-2-1', '2-1'); |
| 26 | + base.create('/level-1/level-2/file-2-2', '2-2'); |
| 27 | + base.create('/level-1/level-2/file-2-3', '2-3'); |
| 28 | + |
| 29 | + scoped = new ScopedTree(base, 'level-1'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('supports exists', () => { |
| 33 | + expect(scoped.exists('/file-1-1')).toBeTruthy(); |
| 34 | + expect(scoped.exists('file-1-1')).toBeTruthy(); |
| 35 | + expect(scoped.exists('/level-2/file-2-1')).toBeTruthy(); |
| 36 | + expect(scoped.exists('level-2/file-2-1')).toBeTruthy(); |
| 37 | + |
| 38 | + expect(scoped.exists('/file-1-4')).toBeFalsy(); |
| 39 | + expect(scoped.exists('file-1-4')).toBeFalsy(); |
| 40 | + |
| 41 | + expect(scoped.exists('/file-0-1')).toBeFalsy(); |
| 42 | + expect(scoped.exists('file-0-1')).toBeFalsy(); |
| 43 | + expect(scoped.exists('/level-1/file-1-1')).toBeFalsy(); |
| 44 | + expect(scoped.exists('level-1/file-1-1')).toBeFalsy(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('supports read', () => { |
| 48 | + expect(scoped.read('/file-1-2')).not.toBeNull(); |
| 49 | + expect(scoped.read('file-1-2')).not.toBeNull(); |
| 50 | + |
| 51 | + const test = new UnitTestTree(scoped); |
| 52 | + expect(test.readContent('/file-1-2')).toBe('1-2'); |
| 53 | + expect(test.readContent('file-1-2')).toBe('1-2'); |
| 54 | + |
| 55 | + expect(scoped.read('/file-0-2')).toBeNull(); |
| 56 | + expect(scoped.read('file-0-2')).toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('supports create', () => { |
| 60 | + expect(() => scoped.create('/file-1-4', '1-4')).not.toThrow(); |
| 61 | + |
| 62 | + const test = new UnitTestTree(scoped); |
| 63 | + expect(test.readContent('/file-1-4')).toBe('1-4'); |
| 64 | + expect(test.readContent('file-1-4')).toBe('1-4'); |
| 65 | + |
| 66 | + expect(base.exists('/level-1/file-1-4')).toBeTruthy(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('supports delete', () => { |
| 70 | + expect(() => scoped.delete('/file-0-3')).toThrow(); |
| 71 | + |
| 72 | + expect(() => scoped.delete('/file-1-3')).not.toThrow(); |
| 73 | + expect(scoped.exists('/file-1-3')).toBeFalsy(); |
| 74 | + |
| 75 | + expect(base.exists('/level-1/file-1-3')).toBeFalsy(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('supports overwrite', () => { |
| 79 | + expect(() => scoped.overwrite('/file-1-1', '1-1*')).not.toThrow(); |
| 80 | + expect(() => scoped.overwrite('/file-1-4', '1-4*')).toThrow(); |
| 81 | + |
| 82 | + const test = new UnitTestTree(scoped); |
| 83 | + expect(test.readContent('/file-1-1')).toBe('1-1*'); |
| 84 | + expect(test.readContent('file-1-1')).toBe('1-1*'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('supports rename', () => { |
| 88 | + expect(() => scoped.rename('/file-1-1', '/file-1-1-new')).not.toThrow(); |
| 89 | + expect(() => scoped.rename('/file-1-4', '/file-1-4-new')).toThrow(); |
| 90 | + |
| 91 | + const test = new UnitTestTree(scoped); |
| 92 | + expect(test.readContent('/file-1-1-new')).toBe('1-1'); |
| 93 | + expect(test.readContent('file-1-1-new')).toBe('1-1'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('supports get', () => { |
| 97 | + expect(scoped.get('/file-1-1')).not.toBeNull(); |
| 98 | + |
| 99 | + const file = scoped.get('file-1-1'); |
| 100 | + expect(file && file.path as string).toBe('/file-1-1'); |
| 101 | + |
| 102 | + expect(scoped.get('/file-0-1')).toBeNull(); |
| 103 | + expect(scoped.get('file-0-1')).toBeNull(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('supports getDir', () => { |
| 107 | + expect(scoped.getDir('/level-2')).not.toBeNull(); |
| 108 | + |
| 109 | + const dir = scoped.getDir('level-2'); |
| 110 | + expect(dir.path as string).toBe('/level-2'); |
| 111 | + expect(dir.parent).not.toBeNull(); |
| 112 | + const files: string[] = []; |
| 113 | + dir.visit(path => files.push(path)); |
| 114 | + files.sort(); |
| 115 | + expect(files).toEqual([ |
| 116 | + '/level-2/file-2-1', |
| 117 | + '/level-2/file-2-2', |
| 118 | + '/level-2/file-2-3', |
| 119 | + ]); |
| 120 | + }); |
| 121 | + |
| 122 | + it('supports visit', () => { |
| 123 | + const files: string[] = []; |
| 124 | + scoped.visit(path => files.push(path)); |
| 125 | + files.sort(); |
| 126 | + expect(files).toEqual([ |
| 127 | + '/file-1-1', |
| 128 | + '/file-1-2', |
| 129 | + '/file-1-3', |
| 130 | + '/level-2/file-2-1', |
| 131 | + '/level-2/file-2-2', |
| 132 | + '/level-2/file-2-3', |
| 133 | + ]); |
| 134 | + }); |
| 135 | + |
| 136 | + it('supports root', () => { |
| 137 | + expect(scoped.root).not.toBeNull(); |
| 138 | + expect(scoped.root.path as string).toBe('/'); |
| 139 | + expect(scoped.root.parent).toBeNull(); |
| 140 | + }); |
| 141 | +}); |
0 commit comments