loopback-softdelete
Version:
SoftDelete mixin for Loopback.
90 lines (69 loc) • 2.61 kB
JavaScript
/**
* Tests inspired by https://github.com/clarkbw/loopback-ds-timestamp-mixin
*/
var test = require('tap').test;
var app = require('loopback');
// https://github.com/strongloop/loopback-boot/blob/master/lib/executor.js#L57-L71
// the loopback-boot module patches in the loopback attribute so we can assume the same
app.loopback = require('loopback');
var dataSource = app.createDataSource({
connector: app.Memory
});
// import soft delete mixin
require('../')(app);
test('loopback soft delete', function (tap) {
'use strict';
tap.test('deletedAt', function (t) {
var Book = dataSource.createModel('Book',
{name: String, type: String},
{mixins: {SoftDelete: true}}
);
t.test('should be no records found on database', function (tt) {
Book.create({name: 'book 1', type: 'fiction'}, function (err, book) {
Book.destroyAll(function () {
Book.count(function (err, count) {
tt.equal(0, count);
tt.end();
});
});
});
});
t.test('should be records found on database', function (tt) {
Book.create({name: 'book 1', type: 'fiction'}, function (err, book) {
Book.destroyAll(function () {
Book.count({isDeleted: true}, function (err, count) {
tt.notEqual(0, count);
tt.end();
});
});
});
});
t.end();
});
tap.test('isDeleted', function (t) {
var Book = dataSource.createModel('Book',
{name: String, type: String},
{mixins: {SoftDelete: true}}
);
t.test('should be with isDeleted false', function (tt) {
Book.create({name: 'book 1', type: 'fiction'}, function (err, book) {
tt.equal(false, book.isDeleted);
tt.end();
});
});
t.test('should be with isDeleted true', function (tt) {
Book.create({name: 'book 1', type: 'fiction'}, function (err, book) {
var bookId = book.id;
Book.destroyById(bookId, function () {
Book.find({ where: {id: bookId, isDeleted: true} }, function (err, b) {
b = b.shift();
tt.equal(true, b.isDeleted);
tt.end();
});
});
});
});
t.end();
});
tap.end();
});