Files
habitica/test/api/v3/unit/libs/collectionManipulators.test.js
Matteo Pagliazzi 74ba55c20b Upgrade tests tools and lint migrations and scripts (part 2) (#9998)
* upgrade gulp-babel

* upgrade babel-eslint

* upgrade eslint-friendly-formatter

* start upgrading chai

* start to upgrade eslint

* restore skipped tests

* start to upgrqde monk

* fix linting and remove unused file

* fix mocha notifications, and common tests

* fix unit tests

* start to fix initrgration tests

* more integration tests fixes

* upgrade monk to latest version

* lint /scripts

* migrations: start moving to /archive unused migrations and run eslint with --fix

* lint migrations

* fix more integration tests

* fix test
2018-02-17 18:11:24 +01:00

89 lines
2.2 KiB
JavaScript

import mongoose from 'mongoose';
import {
removeFromArray,
} from '../../../../../website/server/libs/collectionManipulators';
describe('Collection Manipulators', () => {
describe('removeFromArray', () => {
it('removes element from array', () => {
let array = ['a', 'b', 'c', 'd'];
removeFromArray(array, 'c');
expect(array).to.not.include('c');
});
it('removes object from array', () => {
let array = [
{ id: 'a', foo: 'bar' },
{ id: 'b', foo: 'bar' },
{ id: 'c', foo: 'bar' },
{ id: 'd', foo: 'bar' },
{ id: 'e', foo: 'bar' },
];
removeFromArray(array, { id: 'c' });
expect(array).to.not.include({ id: 'c', foo: 'bar' });
});
it('does not change array if value is not found', () => {
let array = ['a', 'b', 'c', 'd'];
removeFromArray(array, 'z');
expect(array).to.have.a.lengthOf(4);
expect(array[0]).to.eql('a');
expect(array[1]).to.eql('b');
expect(array[2]).to.eql('c');
expect(array[3]).to.eql('d');
});
it('returns the removed element', () => {
let array = ['a', 'b', 'c'];
let result = removeFromArray(array, 'b');
expect(result).to.eql('b');
});
it('returns the removed object element', () => {
let array = [
{ id: 'a', foo: 'bar' },
{ id: 'b', foo: 'bar' },
{ id: 'c', foo: 'bar' },
{ id: 'd', foo: 'bar' },
{ id: 'e', foo: 'bar' },
];
let result = removeFromArray(array, { id: 'c' });
expect(result).to.eql({ id: 'c', foo: 'bar' });
});
it('returns false if item is not found', () => {
let array = ['a', 'b', 'c'];
let result = removeFromArray(array, 'z');
expect(result).to.eql(false);
});
it('persists removal of element when mongoose document is saved', async () => {
let schema = new mongoose.Schema({
array: Array,
});
let Model = mongoose.model('ModelToTestRemoveFromArray', schema);
let model = await new Model({
array: ['a', 'b', 'c'],
}).save(); // Initial creation
removeFromArray(model.array, 'b');
let savedModel = await model.save();
expect(savedModel.array).to.not.include('b');
});
});
});