Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete all stubs #89

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
npm-debug.log
data/*
.idea
yarn.lock
4 changes: 1 addition & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ Stubby.prototype.delete = function (id, callback) {

setTimeout(function () {
if (typeof id === 'function') {
delete self.endpoints.db;
self.endpoints.db = {};
callback();
self.endpoints.deleteAll(callback);
} else {
self.endpoints.delete(id, callback);
}
Expand Down
9 changes: 9 additions & 0 deletions src/models/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ Endpoints.prototype.delete = function (id, callback) {
callback();
};

Endpoints.prototype.deleteAll = function (callback) {
if (callback == null) { callback = noop; }

delete this.db;
this.db = {};

callback();
};

Endpoints.prototype.gather = function (callback) {
var id;
var all = [];
Expand Down
8 changes: 5 additions & 3 deletions src/portals/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ Admin.prototype.goDELETE = function (request, response) {
var id = this.getId(request.url);
var self = this;

if (!id) { return this.notSupported(response); }

function callback (err) {
if (err) { self.notFound(response); } else { self.noContent(response); }
}

this.endpoints.delete(id, callback);
if (id) {
this.endpoints.delete(id, callback);
} else {
Copy link
Owner

Choose a reason for hiding this comment

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

The only change I would ask is to ensure that the request was to / to delete everything, instead of deleting everything if an id wasn't captured.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No worries 👍

Out of curiosity; what is an example url that might be something without an id? Is it just anything that isn't a number eg. /test? Or with multiple directories in the path? eg. /123/test. I just want to make sure I cover the right scenarios in a test.

Copy link
Owner

Choose a reason for hiding this comment

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

Those are good examples. I think the code as-is may actually work correctly (due to multiple checks against urlPattern) but for a "delete everything" capability it is good to have tests ensuring it works as intended only on / in case other capabilities are added later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I'll sort that out today.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

this.endpoints.deleteAll(callback);
}
};

Admin.prototype.goGET = function (request, response) {
Expand Down
6 changes: 3 additions & 3 deletions test/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('Admin', function () {
retrieve: this.sandbox.spy(),
update: this.sandbox.spy(),
delete: this.sandbox.spy(),
deleteAll: this.sandbox.spy(),
gather: this.sandbox.spy()
};
sut = new Admin(endpoints, true);
Expand Down Expand Up @@ -325,13 +326,12 @@ describe('Admin', function () {
});

describe('goDELETE', function () {
it('should send not supported for the root url', function () {
it('should delete all for the root url', function () {
this.sandbox.stub(sut, 'getId').returns('');
this.sandbox.spy(sut, 'notSupported');

sut.goDELETE(request, response);

assert(sut.notSupported.calledOnce);
assert(endpoints.deleteAll.calledOnce);
});

it('should delete item if id was gathered', function () {
Expand Down
49 changes: 49 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,55 @@ describe('main', function () {
});
});

describe('delete', function () {
it('should call delete all when only a callback is passed', function (done) {
sut.endpoints = {
deleteAll: function (cb) {
cb(null);
}
};

sut.delete(function () {
done();
});
});

it('should call delete all when no params are passed', function (done) {
sut.endpoints = {
deleteAll: function () {
done();
}
};

sut.delete();
});

it('should call delete when an id is passed', function (done) {
sut.endpoints = {
delete: function (id, cb) {
assert.strictEqual(id, '1');
assert.strictEqual(cb, id);
done();
}
};

sut.delete('1');
});

it('should call delete when an id and callback are passed', function (done) {
sut.endpoints = {
delete: function (id, cb) {
assert.strictEqual(id, '1');
cb();
}
};

sut.delete('1', function () {
done();
});
});
});

describe('start', function () {
beforeEach(function () {
options = {};
Expand Down