|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var expect = require('chai').expect; |
| 4 | +var path = require('path'); |
| 5 | +var addBarrelRegistration = require('../../addon/ng2/utilities/barrel-management'); |
| 6 | +var mockFs = require('mock-fs'); |
| 7 | +var existsSync = require('exists-sync'); |
| 8 | +var EOL = require('os').EOL; |
| 9 | + |
| 10 | +var Blueprint = require('ember-cli/lib/models/blueprint'); |
| 11 | + |
| 12 | +describe('barrel-management', () => { |
| 13 | + var blueprint; |
| 14 | + var installationDirectory; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + blueprint = new Blueprint('/'); |
| 18 | + blueprint.project = { |
| 19 | + root: '/' |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + describe('when not shared', () => { |
| 24 | + |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + var mockDrive = { |
| 28 | + '/src/app/my-component': {} |
| 29 | + }; |
| 30 | + mockFs(mockDrive); |
| 31 | + |
| 32 | + installationDirectory = path.join('src', 'app', 'my-component'); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + mockFs.restore(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should do nothing', () => { |
| 40 | + return addBarrelRegistration(blueprint, installationDirectory).then(() => { |
| 41 | + var barrelPath = path.join(installationDirectory, 'index.ts'); |
| 42 | + expect(existsSync(barrelPath)).to.equal(false); |
| 43 | + }); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('no pre-existing barrel', () => { |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + var mockDrive = { |
| 51 | + '/src/app/shared/my-component': {} |
| 52 | + }; |
| 53 | + mockFs(mockDrive); |
| 54 | + |
| 55 | + installationDirectory = path.join('/src/app/shared/my-component'); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + mockFs.restore(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('create barrel from installation dir', () => { |
| 63 | + return addBarrelRegistration(blueprint, installationDirectory).then(() => { |
| 64 | + var fs = require('fs'); |
| 65 | + var barrelPath = path.join(installationDirectory, '..', 'index.ts'); |
| 66 | + expect(existsSync(barrelPath)).to.equal(true); |
| 67 | + var contents = fs.readFileSync(barrelPath, 'utf8'); |
| 68 | + var expectedContents = `export * from './my-component';${EOL}`; |
| 69 | + expect(contents).to.equal(expectedContents); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('create barrel from installation dir with file name', () => { |
| 74 | + return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => { |
| 75 | + var fs = require('fs'); |
| 76 | + var barrelPath = path.join(installationDirectory, '..', 'index.ts'); |
| 77 | + expect(existsSync(barrelPath)).to.equal(true); |
| 78 | + var contents = fs.readFileSync(barrelPath, 'utf8'); |
| 79 | + var expectedContents = `export * from './my-component/my-smaller-component';${EOL}`; |
| 80 | + expect(contents).to.equal(expectedContents); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + }); |
| 85 | + |
| 86 | + describe('pre-existing barrel', () => { |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + var mockDrive = { |
| 90 | + '/src/app/shared': { |
| 91 | + 'my-component': {}, |
| 92 | + 'index.ts': `export * from './another-component${EOL}export * from './other-component${EOL}` |
| 93 | + } |
| 94 | + }; |
| 95 | + mockFs(mockDrive); |
| 96 | + |
| 97 | + installationDirectory = path.join('/src/app/shared/my-component'); |
| 98 | + }); |
| 99 | + |
| 100 | + afterEach(() => { |
| 101 | + mockFs.restore(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('update barrel from installation dir', () => { |
| 105 | + return addBarrelRegistration(blueprint, installationDirectory).then(() => { |
| 106 | + var fs = require('fs'); |
| 107 | + var barrelPath = path.join(installationDirectory, '..', 'index.ts'); |
| 108 | + expect(existsSync(barrelPath)).to.equal(true); |
| 109 | + var contents = fs.readFileSync(barrelPath, 'utf8'); |
| 110 | + var expectedContents = `export * from './another-component${EOL}export * from './my-component';${EOL}export * from './other-component${EOL}`; |
| 111 | + expect(contents).to.equal(expectedContents); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('updateA barrel from installation dir with file name', () => { |
| 116 | + return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => { |
| 117 | + var fs = require('fs'); |
| 118 | + var barrelPath = path.join(installationDirectory, '..', 'index.ts'); |
| 119 | + expect(existsSync(barrelPath)).to.equal(true); |
| 120 | + var contents = fs.readFileSync(barrelPath, 'utf8'); |
| 121 | + var expectedContents = `export * from './another-component${EOL}export * from './my-component/my-smaller-component';${EOL}export * from './other-component${EOL}`; |
| 122 | + expect(contents).to.equal(expectedContents); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + }); |
| 127 | + |
| 128 | + describe('pre-existing barrel with export already defined', () => { |
| 129 | + |
| 130 | + beforeEach(() => { |
| 131 | + var mockDrive = { |
| 132 | + '/src/app/shared': { |
| 133 | + 'my-component': {}, |
| 134 | + 'index.ts': `export * from './other-component${EOL}export * from './my-component';${EOL}export * from './another-component${EOL}export * from './my-component/my-smaller-component';${EOL}` |
| 135 | + } |
| 136 | + }; |
| 137 | + mockFs(mockDrive); |
| 138 | + |
| 139 | + installationDirectory = path.join('/src/app/shared/my-component'); |
| 140 | + }); |
| 141 | + |
| 142 | + afterEach(() => { |
| 143 | + mockFs.restore(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('update barrel from installation dir should add nothing', () => { |
| 147 | + return addBarrelRegistration(blueprint, installationDirectory).then(() => { |
| 148 | + var fs = require('fs'); |
| 149 | + var barrelPath = path.join(installationDirectory, '..', 'index.ts'); |
| 150 | + expect(existsSync(barrelPath)).to.equal(true); |
| 151 | + var contents = fs.readFileSync(barrelPath, 'utf8'); |
| 152 | + var expectedContents = `export * from './another-component${EOL}export * from './my-component';${EOL}export * from './my-component/my-smaller-component';${EOL}export * from './other-component${EOL}`; |
| 153 | + expect(contents).to.equal(expectedContents); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + it('update barrel from installation dir with file name should add nothing', () => { |
| 158 | + return addBarrelRegistration(blueprint, installationDirectory, 'my-smaller-component').then(() => { |
| 159 | + var fs = require('fs'); |
| 160 | + var barrelPath = path.join(installationDirectory, '..', 'index.ts'); |
| 161 | + expect(existsSync(barrelPath)).to.equal(true); |
| 162 | + var contents = fs.readFileSync(barrelPath, 'utf8'); |
| 163 | + var expectedContents = `export * from './another-component${EOL}export * from './my-component';${EOL}export * from './my-component/my-smaller-component';${EOL}export * from './other-component${EOL}`; |
| 164 | + expect(contents).to.equal(expectedContents); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + }); |
| 169 | +}); |
0 commit comments