mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 23:27:26 +01:00
tests: Change baseModel test to more accurately reflect use case
This commit is contained in:
@@ -1,23 +1,16 @@
|
|||||||
import baseModel from '../../../../../website/src/libs/api-v3/baseModel';
|
import baseModel from '../../../../../website/src/libs/api-v3/baseModel';
|
||||||
|
import { Schema } from 'mongoose';
|
||||||
|
|
||||||
describe('Base model plugin', () => {
|
describe('Base model plugin', () => {
|
||||||
let schema = {
|
let schema;
|
||||||
add () {
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
statics: {},
|
|
||||||
options: {},
|
|
||||||
pre () {
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
schema = new Schema();
|
||||||
sandbox.stub(schema, 'add');
|
sandbox.stub(schema, 'add');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('adds a _id field to the schema', () => {
|
it('adds a _id field to the schema', () => {
|
||||||
baseModel(schema);
|
schema.plugin(baseModel);
|
||||||
|
|
||||||
expect(schema.add).to.be.calledWith(sinon.match({
|
expect(schema.add).to.be.calledWith(sinon.match({
|
||||||
_id: sinon.match.object,
|
_id: sinon.match.object,
|
||||||
@@ -25,13 +18,13 @@ describe('Base model plugin', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('can add timestamps fields', () => {
|
it('can add timestamps fields', () => {
|
||||||
baseModel(schema, {timestamps: true});
|
schema.plugin(baseModel, {timestamps: true});
|
||||||
|
|
||||||
expect(schema.add).to.be.calledTwice;
|
expect(schema.add).to.be.calledTwice;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can sanitize input objects', () => {
|
it('can sanitize input objects', () => {
|
||||||
baseModel(schema, {
|
schema.plugin(baseModel, {
|
||||||
noSet: ['noUpdateForMe'],
|
noSet: ['noUpdateForMe'],
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -44,7 +37,7 @@ describe('Base model plugin', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('accepts an array of additional fields to sanitize at runtime', () => {
|
it('accepts an array of additional fields to sanitize at runtime', () => {
|
||||||
baseModel(schema, {
|
schema.plugin(baseModel, {
|
||||||
noSet: ['noUpdateForMe'],
|
noSet: ['noUpdateForMe'],
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -58,7 +51,7 @@ describe('Base model plugin', () => {
|
|||||||
|
|
||||||
|
|
||||||
it('can make fields private', () => {
|
it('can make fields private', () => {
|
||||||
baseModel(schema, {
|
schema.plugin(baseModel, {
|
||||||
private: ['amPrivate'],
|
private: ['amPrivate'],
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -76,7 +69,7 @@ describe('Base model plugin', () => {
|
|||||||
toJSONTransform: sandbox.stub().returns(true),
|
toJSONTransform: sandbox.stub().returns(true),
|
||||||
};
|
};
|
||||||
|
|
||||||
baseModel(schema, options);
|
schema.plugin(baseModel, options);
|
||||||
|
|
||||||
let objToTransform = {ok: true, amPrivate: true};
|
let objToTransform = {ok: true, amPrivate: true};
|
||||||
let privatized = schema.options.toJSON.transform({}, objToTransform);
|
let privatized = schema.options.toJSON.transform({}, objToTransform);
|
||||||
@@ -91,7 +84,7 @@ describe('Base model plugin', () => {
|
|||||||
sanitizeTransform: sandbox.stub().returns(true),
|
sanitizeTransform: sandbox.stub().returns(true),
|
||||||
};
|
};
|
||||||
|
|
||||||
baseModel(schema, options);
|
schema.plugin(baseModel, options);
|
||||||
|
|
||||||
expect(schema.options.toJSON.transform).to.exist;
|
expect(schema.options.toJSON.transform).to.exist;
|
||||||
let objToSanitize = {ok: true, noUpdateForMe: true};
|
let objToSanitize = {ok: true, noUpdateForMe: true};
|
||||||
|
|||||||
Reference in New Issue
Block a user