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 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
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
10 changes: 7 additions & 3 deletions src/portals/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ 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 if (request.url === '/') {
this.endpoints.deleteAll(callback);
} else {
this.notSupported(response);
}
};

Admin.prototype.goGET = function (request, response) {
Expand Down
19 changes: 16 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,25 @@ describe('Admin', function () {
});

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

request.url = '/';
sut.goDELETE(request, response);

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

['/test', '/1/test'].forEach(function (url) {
it('should not delete all stubs when calling from non-root urls: ' + url, function () {
this.sandbox.stub(sut, 'getId').returns('');

request.url = url;
sut.goDELETE(request, response);

assert(endpoints.deleteAll.notCalled);
assert.strictEqual(response.statusCode, 405);
});
});

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