Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@
require('app').controller('ContainerFilesController', ContainerFilesController);

function ContainerFilesController(
$q,
loadingPromises,
promisify,
errs,
Expand DownExpand Up@@ -161,25 +162,39 @@ function ContainerFilesController(
},
deleteFile: function (containerFile) {
$rootScope.$broadcast('close-popovers');

var file = containerFile.fileModel || self.state.contextVersion.rootDir.contents.models.find(function (fileModel) {
return fileModel.attrs.name === containerFile.name;
});
if (file) {
var containerIndex = self.state.containerFiles.indexOf(containerFile);
if (containerIndex > -1) {
self.state.containerFiles.splice(containerIndex, 1);
}

return loadingPromises.add('editServerModal',
promisify(file, 'destroy')()
.then(function () {
return updateDockerfileFromState(self.state);
})
.catch(errs.handler)
);
}

return loadingPromises.add(
'editServerModal',
$q.when()
.then(function () {
if (containerFile.fileModel) {
return containerFile.fileModel;
}
return promisify(self.state.contextVersion.rootDir.contents, 'fetch')()
.then(function () {
return self.state.contextVersion.rootDir.contents.models.find(function (fileModel) {
return fileModel.attrs.name === containerFile.name;
});
});
})
.then(function (file) {
if (file) {
return promisify(file, 'destroy')()
.then(function () {
return promisify(self.state.contextVersion.rootDir.contents, 'fetch')();
});
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little weird that you decided to do these two unrelated things (Removing the file from the array and fetching the file) in the same .then. Maybe moving the .splice to before the promise creation?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like it after, so if the delete fails, it doesn't get removed

})
.then(function () {
var containerIndex = self.state.containerFiles.indexOf(containerFile);
if (containerIndex > -1) {
self.state.containerFiles.splice(containerIndex, 1);
}
})
.then(function () {
return updateDockerfileFromState(self.state);
})
.catch(errs.handler)
);
}
},
data: {}
Expand DownExpand Up@@ -233,33 +248,7 @@ function ContainerFilesController(
);
$rootScope.$broadcast('close-popovers');
},
remove: function (sshKeyFile) {
var file = sshKeyFile.fileModel || self.state.contextVersion.rootDir.contents.models.find(function (fileModel) {
return fileModel.attrs.name === sshKeyFile.name;
});
var containerIndex = self.state.containerFiles.indexOf(sshKeyFile);
if (containerIndex > -1) {
self.state.containerFiles.splice(containerIndex, 1);
}

if (file) {
loadingPromises.add(
'editServerModal',
promisify(file, 'destroy')()
.then(function () {
return promisify(self.state.contextVersion.rootDir.contents, 'fetch')();
})
.then(function () {
return updateDockerfileFromState(self.state);
})
.catch(errs.handler)
);
} else {
loadingPromises.add('editServerModal', updateDockerfileFromState(self.state))
.catch(errs.handler);
}
$rootScope.$broadcast('close-popovers');
}
remove: this.fileUpload.actions.deleteFile
}
},
getFileDate: function (sshKeyFile) {
Expand Down
12 changes: 8 additions & 4 deletions test/unit/controllers/containerFilesController.unit.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,7 @@ describe('ContainerFilesController'.bold.underline.blue, function () {
$provide.factory('promisify', function ($q) {
promisifyMock = sinon.spy(function (obj, key) {
return function () {
console.log('obj', obj, key);
return $q.when(obj[key].apply(obj, arguments));
};
});
Expand DownExpand Up@@ -83,8 +84,8 @@ describe('ContainerFilesController'.bold.underline.blue, function () {
},
rootDir: {
contents: {
create: sinon.spy(),
fetch: sinon.spy()
create: sinon.stub().returns(),
fetch: sinon.stub().returns()
}
}
}
Expand DownExpand Up@@ -521,8 +522,10 @@ describe('ContainerFilesController'.bold.underline.blue, function () {
}
};
CFC.state.containerFiles = [];
$rootScope.$digest();

CFC.fileUpload.actions.deleteFile(containerFile);
$rootScope.$digest();
sinon.assert.calledOnce(closePopoverSpy);
sinon.assert.calledOnce(containerFile.fileModel.destroy);
});
Expand DownExpand Up@@ -609,7 +612,7 @@ describe('ContainerFilesController'.bold.underline.blue, function () {
sinon.assert.calledOnce(loadingPromises.add);
sinon.assert.calledWith(loadingPromises.add, 'editServerModal');
sinon.assert.calledOnce(fileModel.destroy);
sinon.assert.calledOnce(CFC.state.contextVersion.rootDir.contents.fetch);
sinon.assert.calledTwice(CFC.state.contextVersion.rootDir.contents.fetch);
sinon.assert.calledOnce(updateDockerfileFromStateMock);
sinon.assert.calledOnce(closePopoverSpy);
expect(CFC.state.containerFiles.length).to.equal(1);
Expand All@@ -629,6 +632,7 @@ describe('ContainerFilesController'.bold.underline.blue, function () {

sinon.assert.calledOnce(loadingPromises.add);
sinon.assert.calledWith(loadingPromises.add, 'editServerModal');
sinon.assert.calledOnce(CFC.state.contextVersion.rootDir.contents.fetch);
sinon.assert.calledOnce(updateDockerfileFromStateMock);
sinon.assert.calledOnce(closePopoverSpy);
expect(CFC.state.containerFiles.length).to.equal(1);
Expand All@@ -654,7 +658,7 @@ describe('ContainerFilesController'.bold.underline.blue, function () {
sinon.assert.calledOnce(loadingPromises.add);
sinon.assert.calledWith(loadingPromises.add, 'editServerModal');
sinon.assert.calledOnce(fileModel.destroy);
sinon.assert.calledOnce(CFC.state.contextVersion.rootDir.contents.fetch);
sinon.assert.calledTwice(CFC.state.contextVersion.rootDir.contents.fetch);
sinon.assert.calledOnce(updateDockerfileFromStateMock);
sinon.assert.calledOnce(closePopoverSpy);
expect(CFC.state.containerFiles.length).to.equal(1);
Expand Down