|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// This needs to be first so fs module can be mocked correctly. |
| 4 | +let mockFs = require('mock-fs'); |
| 5 | + |
| 6 | +import {expect} from 'chai'; |
| 7 | +import {InsertChange, RemoveChange, ReplaceChange} from '../../addon/ng2/utilities/change'; |
| 8 | +import fs = require('fs'); |
| 9 | + |
| 10 | +let path = require('path'); |
| 11 | +let Promise = require('ember-cli/lib/ext/promise'); |
| 12 | + |
| 13 | +const readFile = Promise.denodeify(fs.readFile); |
| 14 | + |
| 15 | +describe('Change', () => { |
| 16 | + let sourcePath = 'src/app/my-component'; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + let mockDrive = { |
| 20 | + 'src/app/my-component': { |
| 21 | + 'add-file.txt': 'hello', |
| 22 | + 'remove-replace-file.txt': 'import * as foo from "./bar"', |
| 23 | + 'replace-file.txt': 'import { FooComponent } from "./baz"' |
| 24 | + } |
| 25 | + }; |
| 26 | + mockFs(mockDrive); |
| 27 | + }); |
| 28 | + afterEach(() => { |
| 29 | + mockFs.restore(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('InsertChange', () => { |
| 33 | + let sourceFile = path.join(sourcePath, 'add-file.txt'); |
| 34 | + |
| 35 | + it('adds text to the source code', () => { |
| 36 | + let changeInstance = new InsertChange(sourceFile, 6, ' world!'); |
| 37 | + return changeInstance |
| 38 | + .apply() |
| 39 | + .then(() => readFile(sourceFile, 'utf8')) |
| 40 | + .then(contents => { |
| 41 | + expect(contents).to.equal('hello world!'); |
| 42 | + }); |
| 43 | + }); |
| 44 | + it('fails for negative position', () => { |
| 45 | + expect(() => new InsertChange(sourceFile, -6, ' world!')).to.throw(Error); |
| 46 | + }); |
| 47 | + it('adds nothing in the source code if empty string is inserted', () => { |
| 48 | + let changeInstance = new InsertChange(sourceFile, 6, ''); |
| 49 | + return changeInstance |
| 50 | + .apply() |
| 51 | + .then(() => readFile(sourceFile, 'utf8')) |
| 52 | + .then(contents => { |
| 53 | + expect(contents).to.equal('hello'); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('RemoveChange', () => { |
| 59 | + let sourceFile = path.join(sourcePath, 'remove-replace-file.txt'); |
| 60 | + |
| 61 | + it('removes given text from the source code', () => { |
| 62 | + let changeInstance = new RemoveChange(sourceFile, 9, 'as foo'); |
| 63 | + return changeInstance |
| 64 | + .apply() |
| 65 | + .then(() => readFile(sourceFile, 'utf8')) |
| 66 | + .then(contents => { |
| 67 | + expect(contents).to.equal('import * from "./bar"'); |
| 68 | + }); |
| 69 | + }); |
| 70 | + it('fails for negative position', () => { |
| 71 | + expect(() => new RemoveChange(sourceFile, -6, ' world!')).to.throw(Error); |
| 72 | + }); |
| 73 | + it('does not change the file if told to remove empty string', () => { |
| 74 | + let changeInstance = new RemoveChange(sourceFile, 9, ''); |
| 75 | + return changeInstance |
| 76 | + .apply() |
| 77 | + .then(() => readFile(sourceFile, 'utf8')) |
| 78 | + .then(contents => { |
| 79 | + expect(contents).to.equal('import * as foo from "./bar"'); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('ReplaceChange', () => { |
| 85 | + it('replaces the given text in the source code', () => { |
| 86 | + let sourceFile = path.join(sourcePath, 'remove-replace-file.txt'); |
| 87 | + let changeInstance = new ReplaceChange(sourceFile, 7, '* as foo', '{ fooComponent }'); |
| 88 | + return changeInstance |
| 89 | + .apply() |
| 90 | + .then(() => readFile(sourceFile, 'utf8')) |
| 91 | + .then(contents => { |
| 92 | + expect(contents).to.equal('import { fooComponent } from "./bar"'); |
| 93 | + }); |
| 94 | + }); |
| 95 | + it('fails for negative position', () => { |
| 96 | + let sourceFile = path.join(sourcePath, 'remove-replace-file.txt'); |
| 97 | + expect(() => new ReplaceChange(sourceFile, -6, 'hello', ' world!')).to.throw(Error); |
| 98 | + }); |
| 99 | + it('adds string to the position of an empty string', () => { |
| 100 | + let sourceFile = path.join(sourcePath, 'replace-file.txt'); |
| 101 | + let changeInstance = new ReplaceChange(sourceFile, 9, '', 'BarComponent, '); |
| 102 | + return changeInstance |
| 103 | + .apply() |
| 104 | + .then(() => readFile(sourceFile, 'utf8')) |
| 105 | + .then(contents => { |
| 106 | + expect(contents).to.equal('import { BarComponent, FooComponent } from "./baz"'); |
| 107 | + }); |
| 108 | + }); |
| 109 | + it('removes the given string only if an empty string to add is given', () => { |
| 110 | + let sourceFile = path.join(sourcePath, 'remove-replace-file.txt'); |
| 111 | + let changeInstance = new ReplaceChange(sourceFile, 9, ' as foo', ''); |
| 112 | + return changeInstance |
| 113 | + .apply() |
| 114 | + .then(() => readFile(sourceFile, 'utf8')) |
| 115 | + .then(contents => { |
| 116 | + expect(contents).to.equal('import * from "./bar"'); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments