Skip to content

Commit

Permalink
📝 teardown command
Browse files Browse the repository at this point in the history
  • Loading branch information
weklund committed Oct 19, 2016
1 parent 98d4497 commit 4428243
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
21 changes: 19 additions & 2 deletions src/TearDownCommand.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
class TearDownCommand {
constructor() {

constructor(pathToJsonFile, driverLocator, fs) {
this.pathToJsonFile = pathToJsonFile;
this.driverLocator = driverLocator;
this.fs = fs;
}

run() {
let seededData = this.fs.readFileSync(this.pathToJsonFile);

seededData.reverse();

let drivers = this.driverLocator.drivers();

seededData.forEach((row) => {
let type = row["type"];
if(type in drivers){
let driver = drivers[type];
driver.tearDown();
}else{
throw Error("Seeded data file does not contain a type that you have a driver for");
}
});

}
}
Expand Down
61 changes: 57 additions & 4 deletions test/TearDownCommandTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ let expect = chai.expect;
let TearDownCommand = require('../src/TearDownCommand').TearDownCommand;

describe('Tear down command tests', () => {
it("should throw an error when a seeded data json file doesn't exist", () =>{
let drivers = {};

let driverLocator = {
drivers: function () {
return drivers;
}
};

let fsMock = {
readFileSync: function(){
return [
{
"type": "herpderp",
"properties": {
"name": "Bob Dole"
}
}
];
}
};

let tearDownCommand = new TearDownCommand(
'./path/to/seed.json',
driverLocator,
fsMock
);

expect(tearDownCommand.run.bind(tearDownCommand)).to.throw(
"Seeded data file does not contain a type that you have a driver for"
);

});

it('should destroy all the objects in the dependency chain',() => {

let accountDriverSpy = chai.spy.object(['tearDown']);
Expand All @@ -27,17 +61,36 @@ describe('Tear down command tests', () => {
}
};

let fs = {};
let fsMock = {
readFileSync: function(){
return [
{
"type": "contact",
"properties": {
"name": "Bob Dole"
}
},
{
"type": "account",
"properties": {
"items": [
"Pizza"
]
}
}
];
}
};

let tearDownCommand = new TearDownCommand(
'./path/to/seed.json',
driverLocator,
fs
fsMock
);

tearDownCommand.run();

accountDriverSpy.should.have.been.called.once;
contactDriverSpy.should.have.been.called.once;
accountDriverSpy.tearDown.should.have.been.called.once;
contactDriverSpy.tearDown.should.have.been.called.once;
});
});

0 comments on commit 4428243

Please sign in to comment.