yalento
Version:
An awesome integration of Google Firebase for Angular and Node
220 lines (219 loc) • 13.1 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContactWithoutConstructor = exports.Contact = void 0;
const chai_1 = require("chai");
const mocha_1 = require("mocha");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const __1 = require("..");
class Contact {
constructor(name, lastName, age) {
this.street = 'street';
this.name = name;
this.lastName = lastName;
this.age = age;
}
}
exports.Contact = Contact;
class ContactWithoutConstructor {
constructor() {
this.name = '';
this.lastName = '';
this.street = '';
this.age = 0;
}
}
exports.ContactWithoutConstructor = ContactWithoutConstructor;
mocha_1.describe('RepositoryTest', () => __awaiter(void 0, void 0, void 0, function* () {
mocha_1.it('construct new repository should instantiate with model with and without constructor parameters', () => __awaiter(void 0, void 0, void 0, function* () {
const repository1 = new __1.Repository(Contact, 'Contact');
chai_1.expect(typeof repository1 === 'object').to.be.true;
const repository2 = new __1.Repository(Contact, 'Contact', 'name', 'lastName', 3);
chai_1.expect(typeof repository2 === 'object').to.be.true;
}));
mocha_1.it('destroying a repository should be destroy instance', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
chai_1.expect(typeof repository === 'object').to.be.true;
repository.destroy();
}));
mocha_1.it('construct new repository should instantiate model', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact', 'test1', 'test2', 1);
chai_1.expect(typeof repository === 'object').to.be.true;
const repository2 = new __1.Repository(ContactWithoutConstructor, 'ContactWithoutConstructor');
chai_1.expect(typeof repository2 === 'object').to.be.true;
}));
mocha_1.it('create item of repository should return model', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
chai_1.expect((yield repository.create()).street).to.be.equal('street');
chai_1.expect((yield repository.create({ street: 'test' })).street).to.be.equal('test');
chai_1.expect((yield repository.create({}, 1))['__uuid']).to.be.equal(1);
}));
mocha_1.it('create items with same identifier should create only once', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
chai_1.expect((yield repository.create({}, 1))['__uuid']).to.be.equal(1);
chai_1.expect((yield repository.create({}, 1))['__uuid']).to.be.equal(1);
chai_1.expect(yield repository.select().getResultsAsPromise()).to.be.lengthOf(1);
const repository2 = new __1.Repository(Contact, 'Contact');
yield repository2.createMany([{ __uuid: 2 }, { __uuid: 2 }]);
chai_1.expect(yield repository2.select().getResultsAsPromise()).to.be.lengthOf(1);
}));
mocha_1.it('create many items of repository should return models', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
chai_1.expect(yield repository.createMany([{ age: 1 }, { age: 2 }])).to.lengthOf(2);
chai_1.expect((yield repository.createMany([{ __uuid: 2 }]))[0]['__uuid']).to.be.equal(2);
}));
mocha_1.it('create item of repository by reading default data from sql statement should return model', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
const contact = yield repository.create({ lastName: 'lastName' }, undefined, 'SELECT * FROM Contact WHERE age = 1 AND name LIKE "name"');
chai_1.expect(contact.street).to.equal('street');
chai_1.expect(contact.lastName).to.equal('lastName');
chai_1.expect(contact.age).to.equal(1);
chai_1.expect(contact.name).to.equal('name');
const contacts = yield repository.createMany([{ lastName: 'lastName' }, { lastName: 'lastName' }], 'SELECT * FROM Contact WHERE age = 1 AND name LIKE "name"');
chai_1.expect(contacts).to.be.lengthOf(2);
const contact1 = yield repository.create({}, undefined, 'SELECT * FROM Contact WHERE age = 1 AND name LIKE "name5" AND (age = 2 OR name LIKE "test") AND name LIKE "name5"');
chai_1.expect(contact1.name).to.equal('name5');
const contact2 = yield repository.create({}, undefined, 'SELECT * FROM Contact WHERE 1');
chai_1.expect(contact2.name).to.equal(undefined);
}));
mocha_1.it('querying items from empty repository should return no models', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
chai_1.expect(repository.select().getResults()).to.lengthOf(0);
const repository2 = new __1.Repository(Contact, 'Contact', 'name', 'lastName', 1);
chai_1.expect(repository2.select().getResults()).to.lengthOf(0);
}));
mocha_1.it('querying items from repository should return matching models', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
const contacts1 = repository.select();
yield contacts1.create({ name: 'name', lastName: 'lastName', age: 3 });
chai_1.expect(contacts1.getResults()).to.lengthOf(1);
const contacts2 = repository.select({ where: 'street LIKE "street" and name LIKE "name1"' });
yield contacts2.create({ name: 'name1', age: 1 });
yield contacts2.create({ name: 'name1', age: 2 });
yield contacts2.create({ name: 'name1', age: 2 });
yield contacts2.create({ name: 'name1', age: 3 });
yield contacts2.create({ name: 'name1', age: 4 });
yield contacts2.create({ name: 'name1', age: 5 });
yield contacts2.create({ name: 'name1', age: 6 });
chai_1.expect(contacts2.getResults()).to.lengthOf(7);
const contactsAll = repository.select();
chai_1.expect(yield contactsAll.getResultsAsPromise()).to.be.lengthOf(8);
chai_1.expect(yield repository.select({ limit: 1 }).getResultsAsPromise()).to.be.lengthOf(1);
chai_1.expect(yield repository.select({ offset: 1 }).getResultsAsPromise()).to.be.lengthOf(1);
chai_1.expect(yield repository.select({ offset: 9 }).getResultsAsPromise()).to.be.lengthOf(0);
chai_1.expect(yield repository.select({ limit: 1, offset: 1 }).getResultsAsPromise()).to.be.lengthOf(1);
chai_1.expect((yield repository.select({ orderBy: 'age DESC' }).getResultsAsPromise())[0].age).to.be.equal(6);
chai_1.expect((yield repository.select({ orderBy: 'age ASC' }).getResultsAsPromise())[0].age).to.be.equal(1);
}));
mocha_1.it('querying items with params from repository should return matching models', () => __awaiter(void 0, void 0, void 0, function* () {
const age$ = new rxjs_1.BehaviorSubject(1);
const repository = new __1.Repository(Contact, 'Contact', 'test');
const contacts = repository.select({ where: 'age = ? AND name LIKE ?', params: [age$, 'test'] });
yield repository.create({ age: 1 });
yield repository.create({ age: 2 });
yield repository.create({ age: 2 });
yield repository.create({ age: 3 });
yield repository.create({ age: 3 });
yield repository.create({ age: 3 });
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(1);
age$.next(2);
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(2);
age$.next(3);
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(3);
}));
mocha_1.it('querying items from repository should return observable results', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
const contacts = repository.select();
chai_1.expect(yield contacts.getResults()).to.be.lengthOf(0);
const waitFor = () => __awaiter(void 0, void 0, void 0, function* () {
return new Promise((resolve) => {
const counter = [];
contacts
.getResultsAsObservable()
.pipe(operators_1.takeWhile((results) => {
counter.push(results.length);
return results.length !== 3;
}))
.toPromise()
.then(() => {
resolve(counter);
});
repository.create({ age: 1 });
setTimeout(() => {
repository.create({ age: 2 });
}, 5);
setTimeout(() => {
repository.create({ age: 3 });
}, 50);
});
});
chai_1.expect(yield waitFor()).to.be.deep.eq([1, 2, 3]);
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(3);
chai_1.expect(yield contacts.getResults()).to.be.lengthOf(3);
}));
mocha_1.it('paginator for select items from repository should be applied', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
yield repository.create({ age: 1 });
yield repository.create({ age: 2 });
yield repository.create({ age: 3 });
yield repository.create({ age: 4 });
yield repository.create({ age: 5 });
yield repository.create({ age: 6 });
yield repository.create({ age: 7 });
yield repository.create({ age: 8 });
const contacts = repository.select({}, {
pageSize: 5,
pageSizeOptions: [10],
pageSort: { active: 'age', direction: 'ASC' },
});
chai_1.expect(contacts.getPaginator().getPageSizeOptions()).to.be.lengthOf(2);
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(5);
contacts.getPaginator().setPageIndex(1);
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(3);
contacts.getPaginator().setPageIndex(100);
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(3);
chai_1.expect(repository
.select({}, {
pageSize: 5,
pageSizeOptions: [5, 10],
pageSort: { active: 'age', direction: 'ASC' },
})
.getPaginator()
.getPageSizeOptions()).to.be.lengthOf(2);
}));
mocha_1.it('creating model after repository querying should update results', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
const contacts = repository.select();
chai_1.expect(contacts.getResults()).to.be.lengthOf(0);
yield contacts.create();
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(1);
yield repository.createMany([{ age: 1 }, { age: 2 }]);
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(3);
}));
mocha_1.it('removing model after repository querying should update results', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
const contacts = repository.select();
const contact = yield contacts.create();
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(1);
yield repository.remove(contact);
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(0);
}));
mocha_1.it('updating model after repository querying should update results', () => __awaiter(void 0, void 0, void 0, function* () {
const repository = new __1.Repository(Contact, 'Contact');
const contacts = repository.select({ where: 'age = 1' });
const contact = yield contacts.create();
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(0);
contact.age = 1;
yield repository.update(contact);
chai_1.expect(yield contacts.getResultsAsPromise()).to.be.lengthOf(1);
}));
}));