diff --git a/test/integration/account.accessor.http.spec.js b/test/integration/account.accessor.http.spec.js new file mode 100644 index 0000000..bd031e3 --- /dev/null +++ b/test/integration/account.accessor.http.spec.js @@ -0,0 +1,221 @@ +import _ from 'lodash'; +import request from 'supertest'; +import { expect, faker, clear, create } from '@lykmapipo/mongoose-test-helpers'; +import { app, mount } from '@lykmapipo/express-common'; +import { Jurisdiction } from '@codetanzania/majifix-jurisdiction'; +import account from '../../src'; + +const { Account, apiVersion, accountRouter } = account; + +mount(accountRouter); +describe('Account accessor Rest API', () => { + const jurisdiction = Jurisdiction.fake(); + let customerAccount = Account.fake(); + customerAccount.set({ jurisdiction }); + + before(done => clear(Jurisdiction, Account, done)); + + before(done => create(jurisdiction, done)); + + before(done => create(customerAccount, done)); + + it('should handle POST /accounts/:id/accessors', done => { + const accessor = Account.fake().accessors[0].toObject(); + + request(app) + .post(`/${apiVersion}/accounts/${customerAccount._id}/accessors`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .send(accessor) + .expect(200) + .end((error, response) => { + expect(error).to.not.exist; + expect(response).to.exist; + + const updates = new Account(response.body); + + expect(updates._id).to.exist; + expect(updates._id).to.be.eql(customerAccount._id); + expect(updates.number).to.be.equal(customerAccount.number); + expect(updates.name).to.be.equal(customerAccount.name); + + expect(updates.accessors.length).to.be.above( + customerAccount.accessors.length + ); + + customerAccount = updates; + + done(error, response); + }); + }); + + it('should handle GET /accounts/:id/accessors', done => { + request(app) + .get(`/${apiVersion}/accounts/${customerAccount._id}/accessors`) + .set('Accept', 'application/json') + .expect(200) + .end((error, response) => { + expect(error).to.not.exist; + expect(response).to.exist; + + // assert payload + const result = new Account(response.body); + expect(result._id).to.exist; + expect(result._id).to.be.eql(customerAccount._id); + expect(result.number).to.be.equal(customerAccount.number); + expect(result.name).to.be.equal(customerAccount.name); + expect(result.accessors).to.have.length.at.least(1); + done(error, response); + }); + }); + + it('should handle PATCH /accounts/:id/accessors/:phone', done => { + const accessor = customerAccount.accessors[0]; + const { phone } = accessor; + const patch = { phone: faker.phone.phoneNumber() }; + + request(app) + .patch( + `/${apiVersion}/accounts/${customerAccount._id}/accessors/${phone}` + ) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .send(patch) + .expect(200) + .end((error, response) => { + expect(error).to.not.exist; + expect(response).to.exist; + + const patched = response.body; + + expect(patched._id).to.exist; + expect(patched._id).to.be.equal(customerAccount._id.toString()); + expect(patched.number).to.be.equal(customerAccount.number); + expect(patched.name).to.be.equal(customerAccount.name); + + // assert accessor + const _accessor = _.find(patched.accessors, patch); + expect(_accessor).to.exist; + expect(_accessor.name).to.be.eql(accessor.name); + expect(_accessor.email).to.be.eql(accessor.email); + expect(_accessor.phone).to.be.eql(patch.phone); + + customerAccount = patched; + + done(error, response); + }); + }); + + it('should handle PUT /accounts/:id/accessors/:phone', done => { + const accessor = customerAccount.accessors[0]; + const { phone } = accessor; + const put = { phone: faker.phone.phoneNumber() }; + + request(app) + .put(`/${apiVersion}/accounts/${customerAccount._id}/accessors/${phone}`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .send(put) + .expect(200) + .end((error, response) => { + expect(error).to.not.exist; + expect(response).to.exist; + + const puted = response.body; + + expect(puted._id).to.exist; + expect(puted._id).to.be.equal(customerAccount._id.toString()); + expect(puted.number).to.be.equal(customerAccount.number); + expect(puted.name).to.be.equal(customerAccount.name); + + // assert accessor + const _accessor = _.find(puted.accessors, put); + expect(_accessor).to.exist; + expect(_accessor.name).to.be.eql(accessor.name); + expect(_accessor.email).to.be.eql(accessor.email); + expect(_accessor.phone).to.be.eql(put.phone); + + customerAccount = puted; + + done(error, response); + }); + }); + + it('should handle POST /accounts/verify', done => { + const requestor = { + phone: customerAccount.phone, + account: customerAccount.number, + }; + + request(app) + .post(`/${apiVersion}/accounts/verify`) + .set('Accept', 'application/json') + .set('Content-Type', 'application/json') + .send(requestor) + .expect(200) + .end((error, response) => { + expect(error).to.not.exist; + expect(response).to.exist; + + const updates = response.body; + + expect(updates._id).to.exist; + expect(updates._id).to.be.equal(customerAccount._id); + expect(updates.number).to.be.equal(customerAccount.number); + expect(updates.name).to.be.equal(customerAccount.name); + + done(error, response); + }); + }); + + it('should handle DELETE /accounts/:id/accessors/:phone', done => { + const accessor = customerAccount.accessors[0]; + const { phone } = accessor; + request(app) + .delete( + `/${apiVersion}/accounts/${customerAccount._id}/accessors/${phone}` + ) + .set('Accept', 'application/json') + .expect(200) + .end((error, response) => { + expect(error).to.not.exist; + expect(response).to.exist; + const deleted = response.body; + expect(deleted._id).to.exist; + expect(deleted._id).to.be.equal(customerAccount._id); + expect(deleted.number).to.be.equal(customerAccount.number); + expect(deleted.name).to.be.equal(customerAccount.name); + // assert accessors + const _accessor = _.find(deleted.accessors, { phone }); + expect(_accessor).to.not.exist; + customerAccount = deleted; + done(error, response); + }); + }); + + it('should handle HTTP GET on /jurisdictions/:id/accounts', done => { + request(app) + .get( + `/${apiVersion}/jurisdictions/${customerAccount.jurisdiction._id}/accounts` + ) + .set('Accept', 'application/json') + .expect(200) + .end((error, response) => { + expect(error).to.not.exist; + expect(response).to.exist; + + // assert payload + const result = response.body; + expect(result.data).to.exist; + expect(result.total).to.exist; + expect(result.limit).to.exist; + expect(result.skip).to.exist; + expect(result.page).to.exist; + expect(result.pages).to.exist; + expect(result.lastModified).to.exist; + done(error, response); + }); + }); + + after(done => clear(Jurisdiction, Account, done)); +}); diff --git a/test/integration/account.delete.spec.js b/test/integration/account.delete.spec.js index fc14a7c..0d201e7 100644 --- a/test/integration/account.delete.spec.js +++ b/test/integration/account.delete.spec.js @@ -3,63 +3,67 @@ import account from '../../src'; const { Account } = account; -describe('Account', () => { +describe('Account static delete', () => { let customerAccount; + before(done => clear(Account, done)); - describe('static delete', () => { - before(done => { - customerAccount = Account.fake(); - customerAccount.post((error, created) => { - customerAccount = created; - done(error, created); - }); + + before(done => { + customerAccount = Account.fake(); + customerAccount.post((error, created) => { + customerAccount = created; + done(error, created); }); + }); - it('should be able to delete', done => { - Account.del(customerAccount._id, (error, deleted) => { - expect(error).to.not.exist; - expect(deleted).to.exist; - expect(deleted._id).to.eql(customerAccount._id); - done(error, deleted); - }); + it('should be able to delete', done => { + Account.del(customerAccount._id, (error, deleted) => { + expect(error).to.not.exist; + expect(deleted).to.exist; + expect(deleted._id).to.eql(customerAccount._id); + done(error, deleted); }); + }); - it('should throw if not exists', done => { - Account.del(customerAccount._id, (error, deleted) => { - expect(error).to.exist; - // expect(error.status).to.exist; - expect(error.name).to.be.equal('DocumentNotFoundError'); - expect(deleted).to.not.exist; - done(); - }); + it('should throw if not exists', done => { + Account.del(customerAccount._id, (error, deleted) => { + expect(error).to.exist; + // expect(error.status).to.exist; + expect(error.name).to.be.equal('DocumentNotFoundError'); + expect(deleted).to.not.exist; + done(); }); }); - describe('instance delete', () => { - before(done => { - customerAccount = Account.fake(); - customerAccount.post((error, created) => { - customerAccount = created; - done(error, created); - }); + after(done => clear(Account, done)); +}); + +describe('Account instance delete', () => { + let customerAccount; + + before(done => { + customerAccount = Account.fake(); + customerAccount.post((error, created) => { + customerAccount = created; + done(error, created); }); + }); - it('should be able to delete', done => { - customerAccount.del((error, deleted) => { - expect(error).to.not.exist; - expect(deleted).to.exist; - expect(deleted._id).to.eql(customerAccount._id); - done(error, deleted); - }); + it('should be able to delete', done => { + customerAccount.del((error, deleted) => { + expect(error).to.not.exist; + expect(deleted).to.exist; + expect(deleted._id).to.eql(customerAccount._id); + done(error, deleted); }); + }); - it('should throw if not exists', done => { - customerAccount.del((error, deleted) => { - expect(error).to.not.exist; - expect(deleted).to.exist; - expect(deleted._id).to.eql(customerAccount._id); - done(); - }); + it('should throw if not exists', done => { + customerAccount.del((error, deleted) => { + expect(error).to.not.exist; + expect(deleted).to.exist; + expect(deleted._id).to.eql(customerAccount._id); + done(); }); }); diff --git a/test/integration/account.fetch.spec.js b/test/integration/account.fetch.spec.js index a38b86b..33f1cee 100644 --- a/test/integration/account.fetch.spec.js +++ b/test/integration/account.fetch.spec.js @@ -5,315 +5,313 @@ import account from '../../src'; const { Account } = account; -describe('Account', () => { +describe.skip('Account instance fetch', () => {}); + +describe('Account static fetch', () => { let customerAccount; + const jurisdiction = Jurisdiction.fake(); before(done => clear(Jurisdiction, Account, done)); before(done => create(jurisdiction, done)); - describe.skip('instance fetch', () => {}); - - describe('static fetch', () => { - before(done => { - // let account; - customerAccount = Account.fake(); - customerAccount.post((error, created) => { - customerAccount = created; - done(error, created); - }); + before(done => { + customerAccount = Account.fake(); + customerAccount.post((error, created) => { + customerAccount = created; + done(error, created); }); + }); - it('should have fetch capability', () => { - expect(Account.fetch).to.exist; - expect(Account.fetch).to.be.a('function'); - expect(Account.fetch.length).to.be.equal(3); - }); + it('should have fetch capability', () => { + expect(Account.fetch).to.exist; + expect(Account.fetch).to.be.a('function'); + expect(Account.fetch.length).to.be.equal(3); + }); - it('should be able to fetch with invalid args length', done => { - const { identity } = Account.fake(); - Account.fetch(identity, (error, fetched) => { - expect(error).to.not.exist; - expect(fetched).to.exist; - expect(fetched).to.be.empty; - done(error, fetched); - }); + it('should be able to fetch with invalid args length', done => { + const { identity } = Account.fake(); + Account.fetch(identity, (error, fetched) => { + expect(error).to.not.exist; + expect(fetched).to.exist; + expect(fetched).to.be.empty; + done(error, fetched); }); + }); - it('should be able to fetch with invalid args length', done => { - Account.fetch((error, fetched) => { - expect(error).to.not.exist; - expect(fetched).to.exist; - expect(fetched).to.be.empty; - done(error, fetched); - }); + it('should be able to fetch with invalid args length', done => { + Account.fetch((error, fetched) => { + expect(error).to.not.exist; + expect(fetched).to.exist; + expect(fetched).to.be.empty; + done(error, fetched); }); + }); - it('should be able to fetch without provider', done => { - const { identity, fetchedAt } = Account.fake(); - Account.fetch(identity, fetchedAt, (error, fetched) => { - expect(error).to.not.exist; - expect(fetched).to.exist; - expect(fetched).to.be.empty; - done(error, fetched); - }); + it('should be able to fetch without provider', done => { + const { identity, fetchedAt } = Account.fake(); + Account.fetch(identity, fetchedAt, (error, fetched) => { + expect(error).to.not.exist; + expect(fetched).to.exist; + expect(fetched).to.be.empty; + done(error, fetched); }); + }); - it('should be able to fetch with provider', done => { - const { identity, fetchedAt } = Account.fake(); - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(null, { - name: faker.name.findName(), - }); - }; - - Account.fetch(identity, fetchedAt, (error, fetched) => { - expect(error).to.not.exist; - expect(fetched).to.exist; - expect(fetched).to.be.not.be.empty; - expect(fetched.name).to.exist; - expect(fetched.fetchedAt).to.exist; - delete Account.fetchAccount; - done(error, fetched); + it('should be able to fetch with provider', done => { + const { identity, fetchedAt } = Account.fake(); + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(null, { + name: faker.name.findName(), }); + }; + + Account.fetch(identity, fetchedAt, (error, fetched) => { + expect(error).to.not.exist; + expect(fetched).to.exist; + expect(fetched).to.be.not.be.empty; + expect(fetched.name).to.exist; + expect(fetched.fetchedAt).to.exist; + delete Account.fetchAccount; + done(error, fetched); }); + }); - it('should be able to fetch with provider', done => { - const { identity, fetchedAt } = Account.fake(); - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(null, { - name: faker.name.findName(), - bills: [], - accessors: [], - }); - }; - - Account.fetch(identity, fetchedAt, (error, fetched) => { - expect(error).to.not.exist; - expect(fetched).to.exist; - expect(fetched).to.be.not.be.empty; - expect(fetched.name).to.exist; - expect(fetched.fetchedAt).to.exist; - delete Account.fetchAccount; - done(error, fetched); + it('should be able to fetch with provider', done => { + const { identity, fetchedAt } = Account.fake(); + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(null, { + name: faker.name.findName(), + bills: [], + accessors: [], }); + }; + + Account.fetch(identity, fetchedAt, (error, fetched) => { + expect(error).to.not.exist; + expect(fetched).to.exist; + expect(fetched).to.be.not.be.empty; + expect(fetched.name).to.exist; + expect(fetched.fetchedAt).to.exist; + delete Account.fetchAccount; + done(error, fetched); }); + }); - it('should be able to handle fetch with provider error', done => { - const { identity, fetchedAt } = Account.fake(); - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(new Error('No Data')); - }; + it('should be able to handle fetch with provider error', done => { + const { identity, fetchedAt } = Account.fake(); + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(new Error('No Data')); + }; - Account.fetch(identity, fetchedAt, (error, fetched) => { - expect(error).to.exist; - expect(fetched).to.not.exist; - done(); - }); + Account.fetch(identity, fetchedAt, (error, fetched) => { + expect(error).to.exist; + expect(fetched).to.not.exist; + done(); }); + }); - it('should be able to fetch and upsert without provider', done => { - const { identity } = Account.fake(); - Account.fetchAndUpsert(identity, error => { - expect(error).to.exist; - done(); - }); + it('should be able to fetch and upsert without provider', done => { + const { identity } = Account.fake(); + Account.fetchAndUpsert(identity, error => { + expect(error).to.exist; + done(); }); + }); - it('should be able to fetch and upsert with provider', done => { - customerAccount = Account.fake(); - - const { identity } = customerAccount; - const fetched = _.merge( - {}, - { jurisdiction: jurisdiction.name }, - customerAccount.toObject() - ); - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(null, fetched); - }; - - Account.fetchAndUpsert(identity, (error, upserted) => { - expect(error).to.not.exist; - expect(upserted).to.exist; - expect(upserted).to.be.not.be.empty; - expect(upserted.identity).to.be.eql(customerAccount.identity); - expect(upserted.fetchedAt).to.exist; - expect(upserted.updatedAt).to.exist; - // assert jurisdiction - expect(upserted.jurisdiction).to.exist; - expect(upserted.jurisdiction._id).to.be.eql(jurisdiction._id); - delete Account.fetchAccount; - done(error, upserted); - }); + it('should be able to fetch and upsert with provider', done => { + customerAccount = Account.fake(); + + const { identity } = customerAccount; + const fetched = _.merge( + {}, + { jurisdiction: jurisdiction.name }, + customerAccount.toObject() + ); + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(null, fetched); + }; + + Account.fetchAndUpsert(identity, (error, upserted) => { + expect(error).to.not.exist; + expect(upserted).to.exist; + expect(upserted).to.be.not.be.empty; + expect(upserted.identity).to.be.eql(customerAccount.identity); + expect(upserted.fetchedAt).to.exist; + expect(upserted.updatedAt).to.exist; + // assert jurisdiction + expect(upserted.jurisdiction).to.exist; + expect(upserted.jurisdiction._id).to.be.eql(jurisdiction._id); + delete Account.fetchAccount; + done(error, upserted); }); + }); - it('should be able to fetch and upsert with provider', done => { - customerAccount = Account.fake(); - - const { identity } = customerAccount; - const fetched = _.merge( - {}, - { jurisdiction: identity }, - customerAccount.toObject() - ); - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(null, fetched); - }; - - Account.fetchAndUpsert(identity, (error, upserted) => { - expect(error).to.not.exist; - expect(upserted).to.exist; - expect(upserted).to.be.not.be.empty; - expect(upserted.identity).to.be.eql(customerAccount.identity); - expect(upserted.fetchedAt).to.exist; - expect(upserted.updatedAt).to.exist; - // assert jurisdiction - expect(upserted.jurisdiction).to.not.exist; - delete Account.fetchAccount; - done(error, upserted); - }); + it('should be able to fetch and upsert with provider', done => { + customerAccount = Account.fake(); + + const { identity } = customerAccount; + const fetched = _.merge( + {}, + { jurisdiction: identity }, + customerAccount.toObject() + ); + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(null, fetched); + }; + + Account.fetchAndUpsert(identity, (error, upserted) => { + expect(error).to.not.exist; + expect(upserted).to.exist; + expect(upserted).to.be.not.be.empty; + expect(upserted.identity).to.be.eql(customerAccount.identity); + expect(upserted.fetchedAt).to.exist; + expect(upserted.updatedAt).to.exist; + // assert jurisdiction + expect(upserted.jurisdiction).to.not.exist; + delete Account.fetchAccount; + done(error, upserted); }); + }); - it('should be able to fetch and upsert with provider', done => { - const { identity } = Account.fake(); - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(null, { - name: faker.name.findName(), - bills: [], - accessors: [], - }); - }; - - Account.fetchAndUpsert(identity, error => { - expect(error).to.exist; - delete Account.fetchAccount; - done(); + it('should be able to fetch and upsert with provider', done => { + const { identity } = Account.fake(); + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(null, { + name: faker.name.findName(), + bills: [], + accessors: [], }); + }; + + Account.fetchAndUpsert(identity, error => { + expect(error).to.exist; + delete Account.fetchAccount; + done(); }); + }); - it('should be able to handle fetch and upsert with error', done => { - const { identity } = Account.fake(); - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(new Error('No Data')); - }; + it('should be able to handle fetch and upsert with error', done => { + const { identity } = Account.fake(); + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(new Error('No Data')); + }; - Account.fetchAndUpsert(identity, error => { - expect(error).to.exist; - done(); - }); + Account.fetchAndUpsert(identity, error => { + expect(error).to.exist; + done(); }); + }); - it.skip('should be able to handle fetch and upsert existing', done => { - const { identity, fetchedAt } = customerAccount; - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(null, {}); - }; - - Account.fetchAndUpsert(identity, (error, upserted) => { - expect(error).to.not.exist; - expect(upserted).to.exist; - expect(upserted.number).to.be.eql(customerAccount.number); - expect(upserted.fetchedAt).to.exist; - expect(upserted.fetchedAt).to.be.eql(fetchedAt); - done(); - }); + it.skip('should be able to handle fetch and upsert existing', done => { + const { identity, fetchedAt } = customerAccount; + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(null, {}); + }; + + Account.fetchAndUpsert(identity, (error, upserted) => { + expect(error).to.not.exist; + expect(upserted).to.exist; + expect(upserted.number).to.be.eql(customerAccount.number); + expect(upserted.fetchedAt).to.exist; + expect(upserted.fetchedAt).to.be.eql(fetchedAt); + done(); }); + }); - it('should be able to handle fetch and upsert existing', done => { - const { identity, fetchedAt } = customerAccount; - const updates = { name: faker.name.findName() }; - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(null, updates); - }; - - Account.fetchAndUpsert(identity, (error, upserted) => { - expect(error).to.not.exist; - expect(upserted).to.exist; - expect(upserted.number).to.be.eql(customerAccount.number); - expect(upserted.name).to.be.eql(updates.name); - expect(upserted.fetchedAt).to.exist; - expect(upserted.fetchedAt).to.not.be.eql(fetchedAt); - done(); - }); + it('should be able to handle fetch and upsert existing', done => { + const { identity, fetchedAt } = customerAccount; + const updates = { name: faker.name.findName() }; + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(null, updates); + }; + + Account.fetchAndUpsert(identity, (error, upserted) => { + expect(error).to.not.exist; + expect(upserted).to.exist; + expect(upserted.number).to.be.eql(customerAccount.number); + expect(upserted.name).to.be.eql(updates.name); + expect(upserted.fetchedAt).to.exist; + expect(upserted.fetchedAt).to.not.be.eql(fetchedAt); + done(); }); + }); - it('should be able to fetch if get by filter miss', done => { - customerAccount = Account.fake(); - const options = { filter: { number: customerAccount.number } }; - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(null, customerAccount.toObject()); - }; - - Account.get(options, (error, fetched) => { - expect(error).to.not.exist; - expect(fetched).to.exist; - expect(fetched.data).to.be.not.be.empty; - expect(fetched.data).to.have.length.at.least(1); - delete Account.fetchAccount; - done(error, fetched); - }); + it('should be able to fetch if get by filter miss', done => { + customerAccount = Account.fake(); + const options = { filter: { number: customerAccount.number } }; + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(null, customerAccount.toObject()); + }; + + Account.get(options, (error, fetched) => { + expect(error).to.not.exist; + expect(fetched).to.exist; + expect(fetched.data).to.be.not.be.empty; + expect(fetched.data).to.have.length.at.least(1); + delete Account.fetchAccount; + done(error, fetched); }); + }); - it('should be able to fetch if get by filter miss', done => { - customerAccount = Account.fake(); - const options = { filter: { number: customerAccount.number } }; + it('should be able to fetch if get by filter miss', done => { + customerAccount = Account.fake(); + const options = { filter: { number: customerAccount.number } }; - Account.get(options, (error, fetched) => { - expect(error).to.not.exist; - expect(fetched).to.exist; - expect(fetched.data).to.be.be.empty; - done(error, fetched); - }); + Account.get(options, (error, fetched) => { + expect(error).to.not.exist; + expect(fetched).to.exist; + expect(fetched.data).to.be.be.empty; + done(error, fetched); }); + }); - it.skip('should not fetch if get found data', done => { - customerAccount = Account.fake(); - const options = { filter: { number: customerAccount.number } }; + it.skip('should not fetch if get found data', done => { + customerAccount = Account.fake(); + const options = { filter: { number: customerAccount.number } }; - Account.get(options, (error, fetched) => { - expect(error).to.not.exist; - expect(fetched).to.exist; - expect(fetched.data).to.be.not.be.empty; - expect(fetched.data).to.have.length.at.least(1); - done(error, fetched); - }); + Account.get(options, (error, fetched) => { + expect(error).to.not.exist; + expect(fetched).to.exist; + expect(fetched.data).to.be.not.be.empty; + expect(fetched.data).to.have.length.at.least(1); + done(error, fetched); }); + }); - it('should be able to fetch and verify', done => { - customerAccount = Account.fake(); - const { number, phone } = customerAccount; - const requestor = { account: number, phone }; - Account.fetchAccount = (_identity, _fetchedAt, cb) => { - return cb(null, customerAccount.toObject()); - }; - - Account.verify(requestor, (error, verified) => { - expect(error).to.not.exist; - expect(verified).to.exist; - expect(verified).to.be.not.be.empty; - expect(verified.identity).to.be.eql(customerAccount.identity); - expect(verified.fetchedAt).to.exist; - expect(verified.updatedAt).to.exist; - delete Account.fetchAccount; - done(error, verified); - }); + it('should be able to fetch and verify', done => { + customerAccount = Account.fake(); + const { number, phone } = customerAccount; + const requestor = { account: number, phone }; + Account.fetchAccount = (_identity, _fetchedAt, cb) => { + return cb(null, customerAccount.toObject()); + }; + + Account.verify(requestor, (error, verified) => { + expect(error).to.not.exist; + expect(verified).to.exist; + expect(verified).to.be.not.be.empty; + expect(verified.identity).to.be.eql(customerAccount.identity); + expect(verified.fetchedAt).to.exist; + expect(verified.updatedAt).to.exist; + delete Account.fetchAccount; + done(error, verified); }); + }); - it('should be able to fetch and verify existing', done => { - const { number, phone } = customerAccount; - const requestor = { account: number, phone }; - - Account.verify(requestor, (error, verified) => { - expect(error).to.not.exist; - expect(verified).to.exist; - expect(verified).to.be.not.be.empty; - expect(verified.identity).to.be.eql(customerAccount.identity); - expect(verified.fetchedAt).to.exist; - expect(verified.updatedAt).to.exist; - done(error, verified); - }); + it('should be able to fetch and verify existing', done => { + const { number, phone } = customerAccount; + const requestor = { account: number, phone }; + + Account.verify(requestor, (error, verified) => { + expect(error).to.not.exist; + expect(verified).to.exist; + expect(verified).to.be.not.be.empty; + expect(verified.identity).to.be.eql(customerAccount.identity); + expect(verified.fetchedAt).to.exist; + expect(verified.updatedAt).to.exist; + done(error, verified); }); }); diff --git a/test/integration/account.get.spec.js b/test/integration/account.get.spec.js index a93be7a..8ff95e8 100644 --- a/test/integration/account.get.spec.js +++ b/test/integration/account.get.spec.js @@ -3,115 +3,114 @@ import { expect, clear, create } from '@lykmapipo/mongoose-test-helpers'; import account from '../../src'; const { Account } = account; -describe('Account', () => { +describe('Account get', () => { let customerAccounts; + before(done => clear(Account, done)); - describe('get', () => { - before(done => { - customerAccounts = _.map(Account.fake(32), log => { - return log; - }); - create(...customerAccounts, done); + before(done => { + customerAccounts = _.map(Account.fake(32), log => { + return log; }); + create(...customerAccounts, done); + }); - it('should be able to get without options', done => { - Account.get((error, results) => { - expect(error).to.not.exist; - expect(results).to.exist; - expect(results.data).to.exist; - expect(results.data).to.have.length(10); - expect(results.total).to.exist; - expect(results.total).to.be.equal(32); - expect(results.limit).to.exist; - expect(results.limit).to.be.equal(10); - expect(results.skip).to.exist; - expect(results.skip).to.be.equal(0); - expect(results.page).to.exist; - expect(results.page).to.be.equal(1); - expect(results.pages).to.exist; - expect(results.pages).to.be.equal(4); - expect(results.lastModified).to.exist; - expect(_.maxBy(results.data, 'updatedAt').updatedAt).to.be.at.most( - results.lastModified - ); - done(error, results); - }); + it('should be able to get without options', done => { + Account.get((error, results) => { + expect(error).to.not.exist; + expect(results).to.exist; + expect(results.data).to.exist; + expect(results.data).to.have.length(10); + expect(results.total).to.exist; + expect(results.total).to.be.equal(32); + expect(results.limit).to.exist; + expect(results.limit).to.be.equal(10); + expect(results.skip).to.exist; + expect(results.skip).to.be.equal(0); + expect(results.page).to.exist; + expect(results.page).to.be.equal(1); + expect(results.pages).to.exist; + expect(results.pages).to.be.equal(4); + expect(results.lastModified).to.exist; + expect(_.maxBy(results.data, 'updatedAt').updatedAt).to.be.at.most( + results.lastModified + ); + done(error, results); }); + }); - it('should be able to get with options', done => { - const options = { page: 1, limit: 20 }; - Account.get(options, (error, results) => { - expect(error).to.not.exist; - expect(results).to.exist; - expect(results.data).to.exist; - expect(results.data).to.have.length(20); - expect(results.total).to.exist; - expect(results.total).to.be.equal(32); - expect(results.limit).to.exist; - expect(results.limit).to.be.equal(20); - expect(results.skip).to.exist; - expect(results.skip).to.be.equal(0); - expect(results.page).to.exist; - expect(results.page).to.be.equal(1); - expect(results.pages).to.exist; - expect(results.pages).to.be.equal(2); - expect(results.lastModified).to.exist; - expect(_.maxBy(results.data, 'updatedAt').updatedAt).to.be.at.most( - results.lastModified - ); - done(error, results); - }); + it('should be able to get with options', done => { + const options = { page: 1, limit: 20 }; + Account.get(options, (error, results) => { + expect(error).to.not.exist; + expect(results).to.exist; + expect(results.data).to.exist; + expect(results.data).to.have.length(20); + expect(results.total).to.exist; + expect(results.total).to.be.equal(32); + expect(results.limit).to.exist; + expect(results.limit).to.be.equal(20); + expect(results.skip).to.exist; + expect(results.skip).to.be.equal(0); + expect(results.page).to.exist; + expect(results.page).to.be.equal(1); + expect(results.pages).to.exist; + expect(results.pages).to.be.equal(2); + expect(results.lastModified).to.exist; + expect(_.maxBy(results.data, 'updatedAt').updatedAt).to.be.at.most( + results.lastModified + ); + done(error, results); }); + }); - it('should be able to search with options', done => { - const options = { filter: { q: customerAccounts[0].name } }; - Account.get(options, (error, results) => { - expect(error).to.not.exist; - expect(results).to.exist; - expect(results.data).to.exist; - expect(results.data).to.have.length.of.at.least(1); - expect(results.total).to.exist; - expect(results.total).to.be.at.least(1); - expect(results.limit).to.exist; - expect(results.limit).to.be.equal(10); - expect(results.skip).to.exist; - expect(results.skip).to.be.equal(0); - expect(results.page).to.exist; - expect(results.page).to.be.equal(1); - expect(results.pages).to.exist; - expect(results.pages).to.be.equal(1); - expect(results.lastModified).to.exist; - expect(_.maxBy(results.data, 'updatedAt').updatedAt).to.be.at.most( - results.lastModified - ); - done(error, results); - }); + it('should be able to search with options', done => { + const options = { filter: { q: customerAccounts[0].name } }; + Account.get(options, (error, results) => { + expect(error).to.not.exist; + expect(results).to.exist; + expect(results.data).to.exist; + expect(results.data).to.have.length.of.at.least(1); + expect(results.total).to.exist; + expect(results.total).to.be.at.least(1); + expect(results.limit).to.exist; + expect(results.limit).to.be.equal(10); + expect(results.skip).to.exist; + expect(results.skip).to.be.equal(0); + expect(results.page).to.exist; + expect(results.page).to.be.equal(1); + expect(results.pages).to.exist; + expect(results.pages).to.be.equal(1); + expect(results.lastModified).to.exist; + expect(_.maxBy(results.data, 'updatedAt').updatedAt).to.be.at.most( + results.lastModified + ); + done(error, results); }); + }); - it('should parse filter options', done => { - const options = { filter: { number: customerAccounts[0].number } }; - Account.get(options, (error, results) => { - expect(error).to.not.exist; - expect(results).to.exist; - expect(results.data).to.exist; - expect(results.data).to.have.length.of.at.least(1); - expect(results.total).to.exist; - expect(results.total).to.be.at.least(1); - expect(results.limit).to.exist; - expect(results.limit).to.be.equal(10); - expect(results.skip).to.exist; - expect(results.skip).to.be.equal(0); - expect(results.page).to.exist; - expect(results.page).to.be.equal(1); - expect(results.pages).to.exist; - expect(results.pages).to.be.equal(1); - expect(results.lastModified).to.exist; - expect(_.maxBy(results.data, 'updatedAt').updatedAt).to.be.at.most( - results.lastModified - ); - done(error, results); - }); + it('should parse filter options', done => { + const options = { filter: { number: customerAccounts[0].number } }; + Account.get(options, (error, results) => { + expect(error).to.not.exist; + expect(results).to.exist; + expect(results.data).to.exist; + expect(results.data).to.have.length.of.at.least(1); + expect(results.total).to.exist; + expect(results.total).to.be.at.least(1); + expect(results.limit).to.exist; + expect(results.limit).to.be.equal(10); + expect(results.skip).to.exist; + expect(results.skip).to.be.equal(0); + expect(results.page).to.exist; + expect(results.page).to.be.equal(1); + expect(results.pages).to.exist; + expect(results.pages).to.be.equal(1); + expect(results.lastModified).to.exist; + expect(_.maxBy(results.data, 'updatedAt').updatedAt).to.be.at.most( + results.lastModified + ); + done(error, results); }); }); diff --git a/test/integration/account.getById.spec.js b/test/integration/account.getById.spec.js index 3f83701..2c690e5 100644 --- a/test/integration/account.getById.spec.js +++ b/test/integration/account.getById.spec.js @@ -4,73 +4,72 @@ import account from '../../src'; const { Account } = account; -describe('Account', () => { +describe('Account get by id', () => { let customerAccount; + before(done => clear(Account, done)); - describe('get by id', () => { - before(done => { - customerAccount = Account.fake(); - create(customerAccount, done); - }); + before(done => { + customerAccount = Account.fake(); + create(customerAccount, done); + }); - it('should be able to get an instance', done => { - Account.getById(customerAccount._id, (error, found) => { - expect(error).to.not.exist; - expect(found).to.exist; - expect(found._id).to.eql(customerAccount._id); - done(error, found); - }); + it('should be able to get an instance', done => { + Account.getById(customerAccount._id, (error, found) => { + expect(error).to.not.exist; + expect(found).to.exist; + expect(found._id).to.eql(customerAccount._id); + done(error, found); }); + }); - it('should be able to get with options', done => { - const options = { - _id: customerAccount._id, - select: 'number', - }; + it('should be able to get with options', done => { + const options = { + _id: customerAccount._id, + select: 'number', + }; - Account.getById(options, (error, found) => { - expect(error).to.not.exist; - expect(found).to.exist; - expect(found._id).to.eql(customerAccount._id); - expect(found.number).to.exist; + Account.getById(options, (error, found) => { + expect(error).to.not.exist; + expect(found).to.exist; + expect(found._id).to.eql(customerAccount._id); + expect(found.number).to.exist; - // ...assert selection - const fields = _.keys(found.toObject()); - expect(fields).to.have.length(2); - _.map( - [ - 'locale', - 'active', - 'bills', - 'name', - 'phone', - 'email', - 'neighborhood', - 'address', - 'location', - 'createdAt', - 'updatedAt', - ], - field => { - expect(fields).to.not.include(field); - } - ); + // ...assert selection + const fields = _.keys(found.toObject()); + expect(fields).to.have.length(2); + _.map( + [ + 'locale', + 'active', + 'bills', + 'name', + 'phone', + 'email', + 'neighborhood', + 'address', + 'location', + 'createdAt', + 'updatedAt', + ], + field => { + expect(fields).to.not.include(field); + } + ); - done(error, found); - }); + done(error, found); }); + }); - it('should throw if not exists', done => { - const { _id } = Account.fake(); + it('should throw if not exists', done => { + const { _id } = Account.fake(); - Account.getById(_id, (error, found) => { - expect(error).to.exist; - // expect(error.status).to.exist; - expect(error.name).to.be.equal('DocumentNotFoundError'); - expect(found).to.not.exist; - done(); - }); + Account.getById(_id, (error, found) => { + expect(error).to.exist; + // expect(error.status).to.exist; + expect(error.name).to.be.equal('DocumentNotFoundError'); + expect(found).to.not.exist; + done(); }); }); diff --git a/test/integration/account.http.spec.js b/test/integration/account.http.spec.js index 81744d2..8cb9a50 100644 --- a/test/integration/account.http.spec.js +++ b/test/integration/account.http.spec.js @@ -1,421 +1,218 @@ -import _ from 'lodash'; -import request from 'supertest'; -import { expect, faker, clear, create } from '@lykmapipo/mongoose-test-helpers'; -import { app, mount } from '@lykmapipo/express-common'; +import { + clear as clearHttp, + testRouter, +} from '@lykmapipo/express-test-helpers'; +import { + clear as clearDb, + create, + expect, +} from '@lykmapipo/mongoose-test-helpers'; import { Jurisdiction } from '@codetanzania/majifix-jurisdiction'; import account from '../../src'; -const { Account, apiVersion, accountRouter } = account; - -describe('Account', () => { - mount(accountRouter); - - describe('Rest API', () => { - let customerAccount; - const jurisdiction = Jurisdiction.fake(); - - before(done => clear(Jurisdiction, Account, done)); - - before(done => create(jurisdiction, done)); - - it('should handle POST /accounts', done => { - customerAccount = Account.fake(); - customerAccount.jurisdiction = jurisdiction; - - request(app) - .post(`/${apiVersion}/accounts`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(customerAccount) - .expect(201) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - const created = response.body; - - expect(created._id).to.exist; - expect(created.number).to.exist; - expect(created.name).to.exist; - - customerAccount = created; - - done(error, response); - }); - }); - - it('should handle GET /accounts', done => { - request(app) - .get(`/${apiVersion}/accounts`) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - // assert payload - const result = response.body; - expect(result.data).to.exist; - expect(result.data).to.have.length.at.least(1); - expect(result.total).to.exist; - expect(result.limit).to.exist; - expect(result.skip).to.exist; - expect(result.page).to.exist; - expect(result.pages).to.exist; - expect(result.lastModified).to.exist; - done(error, response); - }); - }); - - it('should handle GET /accounts by filters', done => { - request(app) - .get(`/${apiVersion}/accounts?filter[number]=${customerAccount.number}`) - .set('Accept', 'application/json') - .expect(200) - .expect('Content-Type', /json/) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - // assert payload - const result = response.body; - expect(result.data).to.exist; - expect(result.data).to.have.length.at.least(1); - expect(result.total).to.exist; - expect(result.limit).to.exist; - expect(result.skip).to.exist; - expect(result.page).to.exist; - expect(result.pages).to.exist; - expect(result.lastModified).to.exist; - done(error, response); - }); - }); - - it('should handle GET /accounts/:id', done => { - request(app) - .get(`/${apiVersion}/accounts/${customerAccount._id}`) - .set('Accept', 'application/json') - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - const found = response.body; - expect(found._id).to.exist; - expect(found._id).to.be.equal(customerAccount._id); - expect(found.number).to.be.equal(customerAccount.number); - expect(found.name).to.be.equal(customerAccount.name); - - done(error, response); - }); - }); - - it('should handle PATCH /accounts/:id', done => { - const patch = { name: faker.finance.accountName() }; - - request(app) - .patch(`/${apiVersion}/accounts/${customerAccount._id}`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(patch) - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - const patched = response.body; - - expect(patched._id).to.exist; - expect(patched._id).to.be.equal(customerAccount._id); - expect(patched.number).to.be.equal(customerAccount.number); - expect(patched.name).to.be.equal(patch.name); - - customerAccount = patched; - - done(error, response); - }); - }); - - it('should handle PUT /accounts/:id', done => { - const put = { name: faker.finance.accountName() }; - - request(app) - .put(`/${apiVersion}/accounts/${customerAccount._id}`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(put) - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - const puted = response.body; - - expect(puted._id).to.exist; - expect(puted._id).to.be.equal(customerAccount._id); - expect(puted.number).to.be.equal(customerAccount.number); - expect(puted.name).to.be.equal(put.name); - - customerAccount = puted; - - done(error, response); - }); - }); - - it('should handle POST /accounts/:id/accessors', done => { - const accessor = Account.fake().accessors[0].toObject(); - - request(app) - .post(`/${apiVersion}/accounts/${customerAccount._id}/accessors`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(accessor) - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - const updates = response.body; - - expect(updates._id).to.exist; - expect(updates._id).to.be.equal(customerAccount._id); - expect(updates.number).to.be.equal(customerAccount.number); - expect(updates.name).to.be.equal(customerAccount.name); - - expect(updates.accessors.length).to.be.above( - customerAccount.accessors.length - ); - - customerAccount = updates; - - done(error, response); - }); - }); - - it('should handle GET /accounts/:id/accessors', done => { - request(app) - .get(`/${apiVersion}/accounts/${customerAccount._id}/accessors`) - .set('Accept', 'application/json') - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - // assert payload - const result = response.body; - expect(result._id).to.exist; - expect(result._id).to.be.equal(customerAccount._id); - expect(result.number).to.be.equal(customerAccount.number); - expect(result.name).to.be.equal(customerAccount.name); - expect(result.accessors).to.have.length.at.least(1); - done(error, response); - }); - }); - - it('should handle PATCH /accounts/:id/accessors/:phone', done => { - const accessor = customerAccount.accessors[0]; - const { phone } = accessor; - const patch = { phone: faker.phone.phoneNumber() }; - - request(app) - .patch( - `/${apiVersion}/accounts/${customerAccount._id}/accessors/${phone}` - ) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(patch) - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - const patched = response.body; - - expect(patched._id).to.exist; - expect(patched._id).to.be.equal(customerAccount._id.toString()); - expect(patched.number).to.be.equal(customerAccount.number); - expect(patched.name).to.be.equal(customerAccount.name); - - // assert accessor - const _accessor = _.find(patched.accessors, patch); - expect(_accessor).to.exist; - expect(_accessor.name).to.be.eql(accessor.name); - expect(_accessor.email).to.be.eql(accessor.email); - expect(_accessor.phone).to.be.eql(patch.phone); - - customerAccount = patched; - - done(error, response); - }); - }); - - it('should handle PUT /accounts/:id/accessors/:phone', done => { - const accessor = customerAccount.accessors[0]; - const { phone } = accessor; - const put = { phone: faker.phone.phoneNumber() }; - - request(app) - .put( - `/${apiVersion}/accounts/${customerAccount._id}/accessors/${phone}` - ) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(put) - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - const puted = response.body; - - expect(puted._id).to.exist; - expect(puted._id).to.be.equal(customerAccount._id.toString()); - expect(puted.number).to.be.equal(customerAccount.number); - expect(puted.name).to.be.equal(customerAccount.name); - - // assert accessor - const _accessor = _.find(puted.accessors, put); - expect(_accessor).to.exist; - expect(_accessor.name).to.be.eql(accessor.name); - expect(_accessor.email).to.be.eql(accessor.email); - expect(_accessor.phone).to.be.eql(put.phone); - - customerAccount = puted; - - done(error, response); - }); - }); - - it('should handle POST /accounts/verify', done => { - const requestor = { - phone: customerAccount.phone, - account: customerAccount.number, - }; - - request(app) - .post(`/${apiVersion}/accounts/verify`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(requestor) - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - const updates = response.body; - - expect(updates._id).to.exist; - expect(updates._id).to.be.equal(customerAccount._id); - expect(updates.number).to.be.equal(customerAccount.number); - expect(updates.name).to.be.equal(customerAccount.name); - - done(error, response); - }); - }); - - it('should handle POST /accounts/verify', done => { - const requestor = { - phone: customerAccount.phone, - identity: customerAccount.identity, - }; - - request(app) - .post(`/${apiVersion}/accounts/verify`) - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send(requestor) - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; - - const updates = response.body; - - expect(updates._id).to.exist; - expect(updates._id).to.be.equal(customerAccount._id); - expect(updates.number).to.be.equal(customerAccount.number); - expect(updates.name).to.be.equal(customerAccount.name); - - done(error, response); - }); - }); - - it('should handle DELETE /accounts/:id/accessors/:phone', done => { - const accessor = customerAccount.accessors[0]; - const { phone } = accessor; - - request(app) - .delete( - `/${apiVersion}/accounts/${customerAccount._id}/accessors/${phone}` - ) - .set('Accept', 'application/json') - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; +const { Account, accountRouter } = account; + +describe('Account Rest API', () => { + const jurisdiction = Jurisdiction.fake(); + const customerAccount = Account.fake(); + customerAccount.set({ jurisdiction }); + + const options = { + pathList: '/accounts', + pathSingle: '/accounts/:id', + pathSchema: '/accounts/schema/', + pathExport: '/accounts/export/', + }; + + before(done => clearDb(Account, Jurisdiction, done)); + + before(() => clearHttp()); + + before(done => create(jurisdiction, done)); + + it('should handle POST /accounts', done => { + const { testPost } = testRouter(options, accountRouter); + testPost({ ...customerAccount.toObject() }) + .expect(201) + .expect('Content-Type', /json/) + .end((error, { body }) => { + expect(error).to.not.exist; + expect(body).to.exist; + const created = new Account(body); + expect(created._id).to.exist.and.be.eql(customerAccount._id); + expect(created.number).to.exist.and.be.eql(customerAccount.number); + expect(created.name).to.exist.and.be.eql(customerAccount.name); + done(error, body); + }); + }); - const deleted = response.body; + it.skip('should handle GET /accounts/schema', done => { + const { testGetSchema } = testRouter(options, accountRouter); + testGetSchema().expect(200, done); + }); - expect(deleted._id).to.exist; - expect(deleted._id).to.be.equal(customerAccount._id); - expect(deleted.number).to.be.equal(customerAccount.number); - expect(deleted.name).to.be.equal(customerAccount.name); + it.skip('should handle GET /accounts/export', done => { + const { testGetExport } = testRouter(options, accountRouter); + testGetExport() + .expect('Content-Type', 'text/csv; charset=utf-8') + .expect(({ headers }) => { + expect(headers['content-disposition']).to.exist; + }) + .expect(200, done); + }); - // assert accessors - const _accessor = _.find(deleted.accessors, { phone }); - expect(_accessor).to.not.exist; + it('should handle GET /accounts', done => { + const { testGet } = testRouter(options, accountRouter); + testGet() + .expect(200) + .expect('Content-Type', /json/) + .end((error, { body }) => { + expect(error).to.not.exist; + expect(body).to.exist; + expect(body.data).to.exist; + expect(body.total).to.exist; + expect(body.limit).to.exist; + expect(body.skip).to.exist; + expect(body.page).to.exist; + expect(body.pages).to.exist; + // expect(body.lastModified).to.exist; + done(error, body); + }); + }); - customerAccount = deleted; + it('should handle GET /accounts by filters', done => { + const { testGet } = testRouter(options, accountRouter); + testGet() + .query({ filter: { number: customerAccount.number } }) + .expect(200) + .expect('Content-Type', /json/) + .end((error, { body }) => { + expect(error).to.not.exist; + expect(body).to.exist; + expect(body.data).to.exist; + expect(body.total).to.exist; + expect(body.limit).to.exist; + expect(body.skip).to.exist; + expect(body.page).to.exist; + expect(body.pages).to.exist; + // expect(body.lastModified).to.exist; + done(error, body); + }); + }); - done(error, response); - }); - }); + it('should handle GET /accounts/:id', done => { + const { testGet } = testRouter(options, accountRouter); + const params = { id: customerAccount._id.toString() }; + testGet(params) + .expect(200) + .expect('Content-Type', /json/) + .end((error, { body }) => { + expect(error).to.not.exist; + expect(body).to.exist; + const found = new Account(body); + expect(found._id).to.exist.and.be.eql(customerAccount._id); + expect(found.number).to.exist.and.be.eql(customerAccount.number); + expect(found.name).to.exist.and.be.eql(customerAccount.name); + done(error, body); + }); + }); - it('should handle HTTP GET on /jurisdictions/:id/accounts', done => { - request(app) - .get( - `/${apiVersion}/jurisdictions/${customerAccount.jurisdiction._id}/accounts` - ) - .set('Accept', 'application/json') - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; + it('should handle PATCH /accounts/:id', done => { + const { testPatch } = testRouter(options, accountRouter); + const { name } = customerAccount.fakeOnly('name'); + const params = { id: customerAccount._id.toString() }; + testPatch(params, { name }) + .expect(200) + .expect('Content-Type', /json/) + .end((error, { body }) => { + expect(error).to.not.exist; + expect(body).to.exist; + const patched = new Account(body); + expect(patched._id).to.exist.and.be.eql(customerAccount._id); + expect(patched.number).to.exist.and.be.eql(customerAccount.number); + expect(patched.name).to.exist.and.be.eql(customerAccount.name); + done(error, body); + }); + }); - // assert payload - const result = response.body; - expect(result.data).to.exist; - expect(result.total).to.exist; - expect(result.limit).to.exist; - expect(result.skip).to.exist; - expect(result.page).to.exist; - expect(result.pages).to.exist; - expect(result.lastModified).to.exist; - done(error, response); - }); - }); + it('should handle PUT /accounts/:id', done => { + const { testPut } = testRouter(options, accountRouter); + const { name } = customerAccount.fakeOnly('name'); + const params = { id: customerAccount._id.toString() }; + testPut(params, { name }) + .expect(200) + .expect('Content-Type', /json/) + .end((error, { body }) => { + expect(error).to.not.exist; + expect(body).to.exist; + const patched = new Account(body); + expect(patched._id).to.exist.and.be.eql(customerAccount._id); + expect(patched.number).to.exist.and.be.eql(customerAccount.number); + expect(patched.name).to.exist.and.be.eql(customerAccount.name); + done(error, body); + }); + }); - it('should handle HTTP DELETE on /accounts/:id', done => { - request(app) - .delete(`/${apiVersion}/accounts/${customerAccount._id}`) - .set('Accept', 'application/json') - .expect(200) - .end((error, response) => { - expect(error).to.not.exist; - expect(response).to.exist; + it('should handle HTTP DELETE on /accounts/:id', done => { + const { testDelete } = testRouter(options, accountRouter); + const params = { id: customerAccount._id.toString() }; + testDelete(params) + .expect(200) + .expect('Content-Type', /json/) + .end((error, { body }) => { + expect(error).to.not.exist; + expect(body).to.exist; + const patched = new Account(body); + expect(patched._id).to.exist.and.be.eql(customerAccount._id); + expect(patched.number).to.exist.and.be.eql(customerAccount.number); + expect(patched.name).to.exist.and.be.eql(customerAccount.name); + done(error, body); + }); + }); - const deleted = response.body; + it.skip('should handle POST /accounts/verify', done => { + const { testPost } = testRouter(options, accountRouter); + const requestor = { + phone: customerAccount.phone, + account: customerAccount.number, + }; + + testPost(requestor) + .expect(201) + .expect('Content-Type', /json/) + .end((error, { body }) => { + expect(error).to.not.exist; + expect(body).to.exist; + const updates = new Account(body); + expect(updates._id).to.exist.and.be.eql(customerAccount._id); + expect(updates.number).to.exist.and.be.eql(customerAccount.number); + expect(updates.name).to.exist.and.be.eql(customerAccount.name); + done(error, body); + }); + }); - expect(deleted._id).to.exist; - expect(deleted._id).to.be.equal(customerAccount._id); - expect(deleted.number).to.be.equal(customerAccount.number); - expect(deleted.name).to.be.equal(customerAccount.name); + it.skip('should handle HTTP GET on /jurisdictions/:id/accounts', done => { + const { testGet } = testRouter(options, accountRouter); + const params = { id: customerAccount._id.toString() }; + testGet(params) + .expect(200) + .expect('Content-Type', /json/) + .end((error, { body }) => { + expect(error).to.not.exist; + expect(body).to.exist; + const result = new Account(body); + expect(result.data).to.exist; + expect(result.total).to.exist; + expect(result.limit).to.exist; + expect(result.skip).to.exist; + expect(result.page).to.exist; + expect(result.pages).to.exist; + // expect(body.lastModified).to.exist; + done(error, body); + }); + }); - done(error, response); - }); - }); + after(() => clearHttp()); - after(done => clear(Jurisdiction, Account, done)); - }); + after(done => clearDb(Jurisdiction, Account, done)); }); diff --git a/test/integration/account.patch.spec.js b/test/integration/account.patch.spec.js index 4f98b3c..3055ef2 100644 --- a/test/integration/account.patch.spec.js +++ b/test/integration/account.patch.spec.js @@ -5,7 +5,7 @@ import account from '../../src'; const { Account } = account; -describe('Account', () => { +describe('Account static patch', () => { let customerAccount; const jurisdiction = Jurisdiction.fake(); @@ -13,74 +13,81 @@ describe('Account', () => { before(done => create(jurisdiction, done)); - describe('static patch', () => { - before(done => { - customerAccount = Account.fake(); - customerAccount.jurisdiction = jurisdiction; + before(done => { + customerAccount = Account.fake(); + customerAccount.jurisdiction = jurisdiction; - customerAccount.post((error, created) => { - customerAccount = created; - done(error, created); - }); + customerAccount.post((error, created) => { + customerAccount = created; + done(error, created); }); + }); - it('should be able to patch', done => { - customerAccount = customerAccount.fakeOnly('name'); + it('should be able to patch', done => { + customerAccount = customerAccount.fakeOnly('name'); - Account.patch(customerAccount._id, customerAccount, (error, updated) => { - expect(error).to.not.exist; - expect(updated).to.exist; - expect(updated._id).to.eql(customerAccount._id); - expect(updated.name).to.eql(customerAccount.name); - done(error, updated); - }); + Account.patch(customerAccount._id, customerAccount, (error, updated) => { + expect(error).to.not.exist; + expect(updated).to.exist; + expect(updated._id).to.eql(customerAccount._id); + expect(updated.name).to.eql(customerAccount.name); + done(error, updated); }); + }); - it('should throw if not exists', done => { - const fake = Account.fake().toObject(); + it('should throw if not exists', done => { + const fake = Account.fake().toObject(); - Account.patch(fake._id, _.omit(fake, '_id'), (error, updated) => { - expect(error).to.exist; - // expect(error.status).to.exist; - expect(error.name).to.be.equal('DocumentNotFoundError'); - expect(updated).to.not.exist; - done(); - }); + Account.patch(fake._id, _.omit(fake, '_id'), (error, updated) => { + expect(error).to.exist; + // expect(error.status).to.exist; + expect(error.name).to.be.equal('DocumentNotFoundError'); + expect(updated).to.not.exist; + done(); }); }); - describe('instance patch', () => { - before(done => { - customerAccount = Account.fake(); - customerAccount.jurisdiction = jurisdiction; + after(done => clear(Jurisdiction, Account, done)); +}); + +describe('Account instance patch', () => { + let customerAccount; + const jurisdiction = Jurisdiction.fake(); + + before(done => clear(Jurisdiction, Account, done)); + + before(done => create(jurisdiction, done)); - customerAccount.post((error, created) => { - customerAccount = created; - done(error, created); - }); + before(done => { + customerAccount = Account.fake(); + customerAccount.jurisdiction = jurisdiction; + + customerAccount.post((error, created) => { + customerAccount = created; + done(error, created); }); + }); - it('should be able to patch', done => { - customerAccount = customerAccount.fakeOnly('name'); + it('should be able to patch', done => { + customerAccount = customerAccount.fakeOnly('name'); - customerAccount.patch((error, updated) => { - expect(error).to.not.exist; - expect(updated).to.exist; - expect(updated._id).to.eql(customerAccount._id); - expect(updated.name).to.eql(customerAccount.name); - done(error, updated); - }); + customerAccount.patch((error, updated) => { + expect(error).to.not.exist; + expect(updated).to.exist; + expect(updated._id).to.eql(customerAccount._id); + expect(updated.name).to.eql(customerAccount.name); + done(error, updated); }); + }); - it('should not throw if not exists', done => { - customerAccount = Account.fake(); + it('should not throw if not exists', done => { + customerAccount = Account.fake(); - customerAccount.patch((error, updated) => { - expect(error).to.not.exist; - expect(updated).to.exist; - expect(updated._id).to.eql(customerAccount._id); - done(); - }); + customerAccount.patch((error, updated) => { + expect(error).to.not.exist; + expect(updated).to.exist; + expect(updated._id).to.eql(customerAccount._id); + done(); }); }); diff --git a/test/integration/account.post.spec.js b/test/integration/account.post.spec.js index 9ba5897..2186172 100644 --- a/test/integration/account.post.spec.js +++ b/test/integration/account.post.spec.js @@ -4,46 +4,53 @@ import account from '../../src'; const { Account } = account; -describe('Account', () => { +describe('Account static post', () => { let customerAccount; + const jurisdiction = Jurisdiction.fake(); before(done => clear(Jurisdiction, Account, done)); before(done => create(jurisdiction, done)); - describe('static post', () => { - it('should be able to post', done => { - customerAccount = Account.fake(); - customerAccount.jurisdiction = jurisdiction; - - Account.post(customerAccount, (error, created) => { - expect(error).to.not.exist; - expect(created).to.exist; - expect(created._id).to.eql(customerAccount._id); - expect(created.jurisdiction._id).to.eql( - customerAccount.jurisdiction._id - ); - expect(created.name).to.eql(customerAccount.name); - expect(created.number).to.eql(customerAccount.number); - done(error, created); - }); + it('should be able to post', done => { + customerAccount = Account.fake(); + customerAccount.jurisdiction = jurisdiction; + + Account.post(customerAccount, (error, created) => { + expect(error).to.not.exist; + expect(created).to.exist; + expect(created._id).to.eql(customerAccount._id); + expect(created.jurisdiction._id).to.eql(customerAccount.jurisdiction._id); + expect(created.name).to.eql(customerAccount.name); + expect(created.number).to.eql(customerAccount.number); + done(error, created); }); }); - describe('instance post', () => { - it('should be able to post', done => { - customerAccount = Account.fake(); - customerAccount.jurisdiction = jurisdiction; - - customerAccount.post((error, created) => { - expect(error).to.not.exist; - expect(created).to.exist; - expect(created._id).to.eql(customerAccount._id); - expect(created.name).to.eql(customerAccount.name); - expect(created.number).to.eql(customerAccount.number); - done(error, created); - }); + after(done => clear(Jurisdiction, Account, done)); +}); + +describe('Account instance post', () => { + let customerAccount; + + const jurisdiction = Jurisdiction.fake(); + + before(done => clear(Jurisdiction, Account, done)); + + before(done => create(jurisdiction, done)); + + it('should be able to post', done => { + customerAccount = Account.fake(); + customerAccount.jurisdiction = jurisdiction; + + customerAccount.post((error, created) => { + expect(error).to.not.exist; + expect(created).to.exist; + expect(created._id).to.eql(customerAccount._id); + expect(created.name).to.eql(customerAccount.name); + expect(created.number).to.eql(customerAccount.number); + done(error, created); }); }); diff --git a/test/integration/account.put.spec.js b/test/integration/account.put.spec.js index efe9178..5ecb185 100644 --- a/test/integration/account.put.spec.js +++ b/test/integration/account.put.spec.js @@ -5,82 +5,89 @@ import account from '../../src'; const { Account } = account; -describe('Account', () => { +describe('Account static put', () => { let customerAccount; + const jurisdiction = Jurisdiction.fake(); before(done => clear(Jurisdiction, Account, done)); before(done => create(jurisdiction, done)); - describe('static put', () => { - // let account; - - before(done => { - customerAccount = Account.fake(); - customerAccount.jurisdiction = jurisdiction; + before(done => { + customerAccount = Account.fake(); + customerAccount.jurisdiction = jurisdiction; - customerAccount.post((error, created) => { - customerAccount = created; - done(error, created); - }); + customerAccount.post((error, created) => { + customerAccount = created; + done(error, created); }); + }); - it('should be able to put', done => { - customerAccount = customerAccount.fakeOnly('name'); + it('should be able to put', done => { + customerAccount = customerAccount.fakeOnly('name'); - Account.put(customerAccount._id, customerAccount, (error, updated) => { - expect(error).to.not.exist; - expect(updated).to.exist; - expect(updated._id).to.eql(customerAccount._id); - expect(updated.name).to.eql(customerAccount.name); - done(error, updated); - }); + Account.put(customerAccount._id, customerAccount, (error, updated) => { + expect(error).to.not.exist; + expect(updated).to.exist; + expect(updated._id).to.eql(customerAccount._id); + expect(updated.name).to.eql(customerAccount.name); + done(error, updated); }); + }); - it('should throw if not exists', done => { - const fake = Account.fake().toObject(); + it('should throw if not exists', done => { + const fake = Account.fake().toObject(); - Account.put(fake._id, _.omit(fake, '_id'), (error, updated) => { - expect(error).to.exist; - // expect(error.status).to.exist; - expect(error.name).to.be.equal('DocumentNotFoundError'); - expect(updated).to.not.exist; - done(); - }); + Account.put(fake._id, _.omit(fake, '_id'), (error, updated) => { + expect(error).to.exist; + // expect(error.status).to.exist; + expect(error.name).to.be.equal('DocumentNotFoundError'); + expect(updated).to.not.exist; + done(); }); }); - describe('instance put', () => { - before(done => { - customerAccount = Account.fake(); - customerAccount.jurisdiction = jurisdiction; + after(done => clear(Jurisdiction, Account, done)); +}); + +describe('Account instance put', () => { + let customerAccount; - customerAccount.post((error, created) => { - customerAccount = created; - done(error, created); - }); + const jurisdiction = Jurisdiction.fake(); + + before(done => clear(Jurisdiction, Account, done)); + + before(done => create(jurisdiction, done)); + + before(done => { + customerAccount = Account.fake(); + customerAccount.jurisdiction = jurisdiction; + + customerAccount.post((error, created) => { + customerAccount = created; + done(error, created); }); + }); - it('should be able to put', done => { - customerAccount = customerAccount.fakeOnly('name'); + it('should be able to put', done => { + customerAccount = customerAccount.fakeOnly('name'); - customerAccount.put((error, updated) => { - expect(error).to.not.exist; - expect(updated).to.exist; - expect(updated._id).to.eql(customerAccount._id); - expect(updated.name).to.eql(customerAccount.name); - done(error, updated); - }); + customerAccount.put((error, updated) => { + expect(error).to.not.exist; + expect(updated).to.exist; + expect(updated._id).to.eql(customerAccount._id); + expect(updated.name).to.eql(customerAccount.name); + done(error, updated); }); + }); - it('should not throw if not exists', done => { - customerAccount.put((error, updated) => { - expect(error).to.not.exist; - expect(updated).to.exist; - expect(updated._id).to.eql(customerAccount._id); - done(); - }); + it('should not throw if not exists', done => { + customerAccount.put((error, updated) => { + expect(error).to.not.exist; + expect(updated).to.exist; + expect(updated._id).to.eql(customerAccount._id); + done(); }); }); diff --git a/test/integration/account.verify.spec.js b/test/integration/account.verify.spec.js index f663470..aadf37e 100644 --- a/test/integration/account.verify.spec.js +++ b/test/integration/account.verify.spec.js @@ -5,7 +5,7 @@ import account from '../../src'; const { Account } = account; -describe('Account', () => { +describe('Account verify', () => { let customerAccount; const jurisdiction = Jurisdiction.fake(); @@ -13,136 +13,134 @@ describe('Account', () => { before(done => create(jurisdiction, done)); - describe('verify', () => { - before(done => { - customerAccount = Account.fake(); - customerAccount.jurisdiction = jurisdiction; + before(done => { + customerAccount = Account.fake(); + customerAccount.jurisdiction = jurisdiction; - customerAccount.post((error, created) => { - customerAccount = created; - done(error, created); - }); + customerAccount.post((error, created) => { + customerAccount = created; + done(error, created); }); + }); - it('should be able to verify access by account', done => { - const accessor = { - account: customerAccount.number, - phone: customerAccount.phone, - }; - - Account.verify(accessor, (error, verified) => { - expect(error).to.not.exist; - expect(verified).to.exist; - expect(verified._id).to.eql(customerAccount._id); - expect(verified.number).to.eql(customerAccount.number); - done(error, verified); - }); + it('should be able to verify access by account', done => { + const accessor = { + account: customerAccount.number, + phone: customerAccount.phone, + }; + + Account.verify(accessor, (error, verified) => { + expect(error).to.not.exist; + expect(verified).to.exist; + expect(verified._id).to.eql(customerAccount._id); + expect(verified.number).to.eql(customerAccount.number); + done(error, verified); }); + }); - it('should be able to verify access by identity', done => { - const accessor = { - identity: customerAccount.identity, - phone: customerAccount.phone, - }; - - Account.verify(accessor, (error, verified) => { - expect(error).to.not.exist; - expect(verified).to.exist; - expect(verified._id).to.eql(customerAccount._id); - expect(verified.number).to.eql(customerAccount.number); - expect(verified.identity).to.eql(customerAccount.identity); - done(error, verified); - }); + it('should be able to verify access by identity', done => { + const accessor = { + identity: customerAccount.identity, + phone: customerAccount.phone, + }; + + Account.verify(accessor, (error, verified) => { + expect(error).to.not.exist; + expect(verified).to.exist; + expect(verified._id).to.eql(customerAccount._id); + expect(verified.number).to.eql(customerAccount.number); + expect(verified.identity).to.eql(customerAccount.identity); + done(error, verified); }); + }); - it('should be able to verify access by account', done => { - const accessor = { - account: customerAccount.number, - phone: customerAccount.accessors[0].phone, - }; - - Account.verify(accessor, (error, verified) => { - expect(error).to.not.exist; - expect(verified).to.exist; - expect(verified._id).to.eql(customerAccount._id); - expect(verified.number).to.eql(customerAccount.number); - done(error, verified); - }); + it('should be able to verify access by account', done => { + const accessor = { + account: customerAccount.number, + phone: customerAccount.accessors[0].phone, + }; + + Account.verify(accessor, (error, verified) => { + expect(error).to.not.exist; + expect(verified).to.exist; + expect(verified._id).to.eql(customerAccount._id); + expect(verified.number).to.eql(customerAccount.number); + done(error, verified); }); + }); - it('should be able to verify access by identity', done => { - const accessor = { - identity: customerAccount.identity, - phone: customerAccount.accessors[0].phone, - }; - - Account.verify(accessor, (error, verified) => { - expect(error).to.not.exist; - expect(verified).to.exist; - expect(verified._id).to.eql(customerAccount._id); - expect(verified.number).to.eql(customerAccount.number); - expect(verified.identity).to.eql(customerAccount.identity); - done(error, verified); - }); + it('should be able to verify access by identity', done => { + const accessor = { + identity: customerAccount.identity, + phone: customerAccount.accessors[0].phone, + }; + + Account.verify(accessor, (error, verified) => { + expect(error).to.not.exist; + expect(verified).to.exist; + expect(verified._id).to.eql(customerAccount._id); + expect(verified.number).to.eql(customerAccount.number); + expect(verified.identity).to.eql(customerAccount.identity); + done(error, verified); }); + }); - it('should be able to verify shallow access by account', done => { - const accessor = { - account: customerAccount.number, - phone: faker.phone.phoneNumber(), - shallow: true, - }; - - Account.verify(accessor, (error, verified) => { - expect(error).to.not.exist; - expect(verified).to.exist; - expect(verified._id).to.eql(customerAccount._id); - expect(verified.number).to.eql(customerAccount.number); - - const _accessor = _.find(verified.accessors, { phone: accessor.phone }); - expect(_accessor).to.exist; - expect(_accessor.verifiedAt).to.not.exist; - expect(_accessor.phone).to.be.equal(accessor.phone); - - done(error, verified); - }); + it('should be able to verify shallow access by account', done => { + const accessor = { + account: customerAccount.number, + phone: faker.phone.phoneNumber(), + shallow: true, + }; + + Account.verify(accessor, (error, verified) => { + expect(error).to.not.exist; + expect(verified).to.exist; + expect(verified._id).to.eql(customerAccount._id); + expect(verified.number).to.eql(customerAccount.number); + + const _accessor = _.find(verified.accessors, { phone: accessor.phone }); + expect(_accessor).to.exist; + expect(_accessor.verifiedAt).to.not.exist; + expect(_accessor.phone).to.be.equal(accessor.phone); + + done(error, verified); }); + }); - it('should be able to verify shallow access by identity', done => { - const accessor = { - identity: customerAccount.identity, - phone: faker.phone.phoneNumber(), - shallow: true, - }; - - Account.verify(accessor, (error, verified) => { - expect(error).to.not.exist; - expect(verified).to.exist; - expect(verified._id).to.eql(customerAccount._id); - expect(verified.number).to.eql(customerAccount.number); - expect(verified.identity).to.eql(customerAccount.identity); - - const _accessor = _.find(verified.accessors, { phone: accessor.phone }); - expect(_accessor).to.exist; - expect(_accessor.verifiedAt).to.not.exist; - expect(_accessor.phone).to.be.equal(accessor.phone); - - done(error, verified); - }); + it('should be able to verify shallow access by identity', done => { + const accessor = { + identity: customerAccount.identity, + phone: faker.phone.phoneNumber(), + shallow: true, + }; + + Account.verify(accessor, (error, verified) => { + expect(error).to.not.exist; + expect(verified).to.exist; + expect(verified._id).to.eql(customerAccount._id); + expect(verified.number).to.eql(customerAccount.number); + expect(verified.identity).to.eql(customerAccount.identity); + + const _accessor = _.find(verified.accessors, { phone: accessor.phone }); + expect(_accessor).to.exist; + expect(_accessor.verifiedAt).to.not.exist; + expect(_accessor.phone).to.be.equal(accessor.phone); + + done(error, verified); }); + }); - it('should be able to restrict access', done => { - const accessor = { - account: customerAccount.number, - phone: faker.phone.phoneNumber(), - }; - - Account.verify(accessor, (error, verified) => { - expect(error).to.exist; - expect(error.status).to.be.eql(202); - expect(verified).to.not.exist; - done(); - }); + it('should be able to restrict access', done => { + const accessor = { + account: customerAccount.number, + phone: faker.phone.phoneNumber(), + }; + + Account.verify(accessor, (error, verified) => { + expect(error).to.exist; + expect(error.status).to.be.eql(202); + expect(verified).to.not.exist; + done(); }); }); diff --git a/test/unit/accessors.schema.spec.js b/test/unit/accessors.schema.spec.js index c598d23..5b3251c 100644 --- a/test/unit/accessors.schema.spec.js +++ b/test/unit/accessors.schema.spec.js @@ -1,97 +1,83 @@ import { expect } from '@lykmapipo/mongoose-test-helpers'; +import { SchemaTypes } from '@lykmapipo/mongoose-common'; import Account from '../../src/account.model'; -describe('Account', () => { - describe('Accessors', () => { - describe('Schema', () => { - it('should be an array of embedded subdocuments', () => { - const { accessors } = Account.schema.tree; - const { instance } = Account.schema.paths.accessors; - const { tree } = Account.schema.tree.accessors[0]; +describe('Account Accessor Schema', () => { + it('should be an array of embedded subdocuments', () => { + const accessors = Account.path('accessors'); - expect(instance).to.be.equal('Array'); - expect(accessors).to.exist; - expect(tree).to.exist; - expect(tree.name).to.exist; - expect(tree.phone).to.exist; - expect(tree.email).to.exist; - expect(tree.locale).to.exist; - expect(tree.verifiedAt).to.exist; - }); + expect(accessors).to.exist; + expect(accessors).to.be.instanceof(SchemaTypes.Array); + }); - it('should have name field', () => { - const { name } = Account.schema.tree.accessors[0].tree; - const { instance } = Account.schema.paths.accessors.schema.paths.name; + it('should have name field', () => { + const name = Account.path('accessors.name'); - expect(instance).to.be.equal('String'); - expect(name).to.exist; - expect(name).to.be.an('object'); - expect(name.type).to.be.a('function'); - expect(name.type.name).to.be.equal('String'); - expect(name.trim).to.be.true; - expect(name.index).to.be.true; - expect(name.fake).to.exist; - }); + expect(name).to.exist; + expect(name).to.be.an.instanceof(SchemaTypes.String); + expect(name.instance).to.be.equal('String'); + expect(name).to.be.an('object'); + expect(name.options.type).to.be.a('function'); + expect(name.options.type.name).to.be.equal('String'); + expect(name.options.trim).to.be.true; + expect(name.options.index).to.be.true; + expect(name.options.fake).to.exist; + }); - it('should have phone field', () => { - const { phone } = Account.schema.tree.accessors[0].tree; - const { instance } = Account.schema.paths.accessors.schema.paths.phone; + it('should have phone field', () => { + const phone = Account.path('accessors.phone'); - expect(instance).to.be.equal('String'); - expect(phone).to.exist; - expect(phone).to.be.an('object'); - expect(phone.type).to.be.a('function'); - expect(phone.type.name).to.be.equal('String'); - expect(phone.trim).to.be.true; - expect(phone.index).to.be.true; - expect(phone.fake).to.exist; - }); + expect(phone).to.exist; + expect(phone).to.be.an.instanceof(SchemaTypes.String); + expect(phone.instance).to.be.equal('String'); + expect(phone).to.be.an('object'); + expect(phone.options.type).to.be.a('function'); + expect(phone.options.type.name).to.be.equal('String'); + expect(phone.options.trim).to.be.true; + expect(phone.options.index).to.be.true; + expect(phone.options.fake).to.exist; + }); - it('should have email field', () => { - const { email } = Account.schema.tree.accessors[0].tree; - const { instance } = Account.schema.paths.accessors.schema.paths.email; + it('should have email field', () => { + const email = Account.path('accessors.email'); - expect(instance).to.be.equal('String'); - expect(email).to.exist; - expect(email).to.be.an('object'); - expect(email.type).to.be.a('function'); - expect(email.type.name).to.be.equal('String'); - expect(email.trim).to.be.true; - expect(email.lowercase).to.be.true; - expect(email.index).to.be.true; - expect(email.fake).to.exist; - }); + expect(email).to.exist; + expect(email).to.be.an.instanceof(SchemaTypes.String); + expect(email.instance).to.be.equal('String'); + expect(email).to.be.an('object'); + expect(email.options.type).to.be.a('function'); + expect(email.options.type.name).to.be.equal('String'); + expect(email.options.trim).to.be.true; + expect(email.options.index).to.be.true; + expect(email.options.lowercase).to.be.true; + expect(email.options.fake).to.exist; + }); - it('should have locale field', () => { - const { locale } = Account.schema.tree.accessors[0].tree; - const { instance } = Account.schema.paths.accessors.schema.paths.locale; + it('should have locale field', () => { + const locale = Account.path('accessors.locale'); - expect(instance).to.be.equal('String'); - expect(locale).to.exist; - expect(locale).to.be.an('object'); - expect(locale.type).to.be.a('function'); - expect(locale.type.name).to.be.equal('String'); - expect(locale.trim).to.be.true; - expect(locale.index).to.be.true; - expect(locale.default).to.exist; - expect(locale.enum).to.exist; - expect(locale.fake).to.exist; - }); + expect(locale).to.exist; + expect(locale).to.be.an.instanceof(SchemaTypes.String); + expect(locale.instance).to.be.equal('String'); + expect(locale).to.be.an('object'); + expect(locale.options.type).to.be.a('function'); + expect(locale.options.type.name).to.be.equal('String'); + expect(locale.options.trim).to.be.true; + expect(locale.options.index).to.be.true; + expect(locale.options.enum).to.exist; + expect(locale.options.default).to.exist; + expect(locale.options.fake).to.exist; + }); - it('should have verifiedAt field', () => { - const { verifiedAt } = Account.schema.tree.accessors[0].tree; - const { - instance, - } = Account.schema.paths.accessors.schema.paths.verifiedAt; + it('should have verifiedAt field', () => { + const verifiedAt = Account.path('accessors.verifiedAt'); - expect(instance).to.be.equal('Date'); - expect(verifiedAt).to.exist; - expect(verifiedAt).to.be.an('object'); - expect(verifiedAt.type).to.be.a('function'); - expect(verifiedAt.type.name).to.be.equal('Date'); - expect(verifiedAt.index).to.be.true; - expect(verifiedAt.fake).to.exist; - }); - }); + expect(verifiedAt).to.exist; + expect(verifiedAt).to.be.an.instanceof(SchemaTypes.Date); + expect(verifiedAt).to.be.an('object'); + expect(verifiedAt.options.type).to.be.a('function'); + expect(verifiedAt.options.type.name).to.be.equal('Date'); + expect(verifiedAt.options.index).to.be.true; + expect(verifiedAt.options.fake).to.exist; }); }); diff --git a/test/unit/account.model.spec.js b/test/unit/account.model.spec.js index 0500ba5..b66b02e 100644 --- a/test/unit/account.model.spec.js +++ b/test/unit/account.model.spec.js @@ -7,239 +7,237 @@ import { import { Jurisdiction } from '@codetanzania/majifix-jurisdiction'; import Account from '../../src/account.model'; -describe('Account', () => { - describe('Instance', () => { - it('`ensureLocation` should be a function', () => { - const account = Account.fake(); - expect(account.ensureLocation).to.exist; - expect(account.ensureLocation).to.be.a('function'); - expect(account.ensureLocation.length).to.be.equal(0); - expect(account.ensureLocation.name).to.be.equal('ensureLocation'); - }); +describe('Account Instance', () => { + it('`ensureLocation` should be a function', () => { + const account = Account.fake(); + expect(account.ensureLocation).to.exist; + expect(account.ensureLocation).to.be.a('function'); + expect(account.ensureLocation.length).to.be.equal(0); + expect(account.ensureLocation.name).to.be.equal('ensureLocation'); + }); - it('should be able to ensure location from jurisdiction boundaries', () => { - const jurisdiction = Jurisdiction.fake(); - jurisdiction.boundaries = { - coordinates: [randomPolygon().coordinates, randomPolygon().coordinates], - }; - - const account = Account.fake(); - account.jurisdiction = jurisdiction; - - // ensure location - const location = account.ensureLocation(); - expect(location).to.exist; - expect(location.type).to.exist; - expect(location.type).to.be.a('string'); - expect(location.type).to.be.equal(TYPE_POINT); - expect(location.coordinates).to.exist; - expect(location.coordinates).to.be.an('array'); - expect(location.coordinates).to.have.length(2); - }); + it('should be able to ensure location from jurisdiction boundaries', () => { + const jurisdiction = Jurisdiction.fake(); + jurisdiction.boundaries = { + coordinates: [randomPolygon().coordinates, randomPolygon().coordinates], + }; + + const account = Account.fake(); + account.jurisdiction = jurisdiction; + + // ensure location + const location = account.ensureLocation(); + expect(location).to.exist; + expect(location.type).to.exist; + expect(location.type).to.be.a('string'); + expect(location.type).to.be.equal(TYPE_POINT); + expect(location.coordinates).to.exist; + expect(location.coordinates).to.be.an('array'); + expect(location.coordinates).to.have.length(2); + }); - it('should be able to ensure location from jurisdiction location', () => { - const jurisdiction = Jurisdiction.fake(); - jurisdiction.location = randomPoint(); - - const account = Account.fake(); - account.jurisdiction = jurisdiction; - - // ensure location - const location = account.ensureLocation(); - expect(location).to.exist; - expect(location.type).to.exist; - expect(location.type).to.be.a('string'); - expect(location.type).to.be.equal(TYPE_POINT); - expect(location.coordinates).to.exist; - expect(location.coordinates).to.be.an('array'); - expect(location.coordinates).to.have.length(2); - }); + it('should be able to ensure location from jurisdiction location', () => { + const jurisdiction = Jurisdiction.fake(); + jurisdiction.location = randomPoint(); + + const account = Account.fake(); + account.jurisdiction = jurisdiction; + + // ensure location + const location = account.ensureLocation(); + expect(location).to.exist; + expect(location.type).to.exist; + expect(location.type).to.be.a('string'); + expect(location.type).to.be.equal(TYPE_POINT); + expect(location.coordinates).to.exist; + expect(location.coordinates).to.be.an('array'); + expect(location.coordinates).to.have.length(2); + }); - it('`ensureUniqueAccessors` should be a function', () => { - const account = Account.fake(); - expect(account.ensureUniqueAccessors).to.exist; - expect(account.ensureUniqueAccessors).to.be.a('function'); - expect(account.ensureUniqueAccessors.length).to.be.equal(0); - expect(account.ensureUniqueAccessors.name).to.be.equal( - 'ensureUniqueAccessors' - ); - }); + it('`ensureUniqueAccessors` should be a function', () => { + const account = Account.fake(); + expect(account.ensureUniqueAccessors).to.exist; + expect(account.ensureUniqueAccessors).to.be.a('function'); + expect(account.ensureUniqueAccessors.length).to.be.equal(0); + expect(account.ensureUniqueAccessors.name).to.be.equal( + 'ensureUniqueAccessors' + ); + }); - it('should be able to ensure unique accessors', () => { - const account = Account.fake(); - const exist = account.accessors.toObject(); + it('should be able to ensure unique accessors', () => { + const account = Account.fake(); + const exist = account.accessors.toObject(); - account.accessors.push(undefined); - account.ensureUniqueAccessors(); + account.accessors.push(undefined); + account.ensureUniqueAccessors(); - expect(account.accessors).to.exist; - expect(account.accessors.toObject()).to.eql(exist); - }); + expect(account.accessors).to.exist; + expect(account.accessors.toObject()).to.eql(exist); + }); - it('should be able to ensure unique accessors', () => { - const account = Account.fake(); - const exist = account.accessors.toObject(); + it('should be able to ensure unique accessors', () => { + const account = Account.fake(); + const exist = account.accessors.toObject(); - account.accessors.push(account.accessors[0].toObject()); - account.ensureUniqueAccessors(); + account.accessors.push(account.accessors[0].toObject()); + account.ensureUniqueAccessors(); - expect(account.accessors).to.exist; - expect(account.accessors.toObject()).to.eql(exist); - }); + expect(account.accessors).to.exist; + expect(account.accessors.toObject()).to.eql(exist); + }); - it('`upsertAccessor` should be a function', () => { - const account = Account.fake(); - expect(account.upsertAccessor).to.exist; - expect(account.upsertAccessor).to.be.a('function'); - expect(account.upsertAccessor.length).to.be.equal(2); - expect(account.upsertAccessor.name).to.be.equal('upsertAccessor'); - }); + it('`upsertAccessor` should be a function', () => { + const account = Account.fake(); + expect(account.upsertAccessor).to.exist; + expect(account.upsertAccessor).to.be.a('function'); + expect(account.upsertAccessor.length).to.be.equal(2); + expect(account.upsertAccessor.name).to.be.equal('upsertAccessor'); + }); - it('should be able to update existing accessor', () => { - const account = Account.fake(); - account.accessors = [account.accessors[0]]; + it('should be able to update existing accessor', () => { + const account = Account.fake(); + account.accessors = [account.accessors[0]]; - const exist = account.accessors.toObject(); + const exist = account.accessors.toObject(); - const accessor = account.accessors[0]; - const updates = { - phone: faker.phone.phoneNumber(), - name: faker.name.findName(), - email: faker.internet.email(), - }; - account.upsertAccessor(accessor.phone, updates); + const accessor = account.accessors[0]; + const updates = { + phone: faker.phone.phoneNumber(), + name: faker.name.findName(), + email: faker.internet.email(), + }; + account.upsertAccessor(accessor.phone, updates); - expect(account.accessors).to.exist; - expect(account.accessors.toObject()).to.not.be.eql(exist); - }); + expect(account.accessors).to.exist; + expect(account.accessors.toObject()).to.not.be.eql(exist); + }); - it('should be able to add new accessor', () => { - const account = Account.fake(); - const exist = account.accessors.toObject(); - - const updates = { - phone: faker.phone.phoneNumber(), - name: faker.name.findName(), - email: faker.internet.email(), - }; - account.upsertAccessor(undefined, updates); - const current = account.accessors.toObject(); - - expect(account.accessors).to.exist; - expect(exist.length < current.length).to.be.true; - expect(current).to.not.be.eql(exist); - }); + it('should be able to add new accessor', () => { + const account = Account.fake(); + const exist = account.accessors.toObject(); + + const updates = { + phone: faker.phone.phoneNumber(), + name: faker.name.findName(), + email: faker.internet.email(), + }; + account.upsertAccessor(undefined, updates); + const current = account.accessors.toObject(); + + expect(account.accessors).to.exist; + expect(exist.length < current.length).to.be.true; + expect(current).to.not.be.eql(exist); + }); - it('should be able to add new accessor', () => { - const account = Account.fake(); - const exist = account.accessors.toObject(); - - const updates = { - phone: faker.phone.phoneNumber(), - name: faker.name.findName(), - email: faker.internet.email(), - }; - account.upsertAccessor(updates.phone, updates); - const current = account.accessors.toObject(); - - expect(account.accessors).to.exist; - expect(exist.length < current.length).to.be.true; - expect(current).to.not.be.eql(exist); - }); + it('should be able to add new accessor', () => { + const account = Account.fake(); + const exist = account.accessors.toObject(); + + const updates = { + phone: faker.phone.phoneNumber(), + name: faker.name.findName(), + email: faker.internet.email(), + }; + account.upsertAccessor(updates.phone, updates); + const current = account.accessors.toObject(); + + expect(account.accessors).to.exist; + expect(exist.length < current.length).to.be.true; + expect(current).to.not.be.eql(exist); + }); - it('`removeAccessor` should be a function', () => { - const account = Account.fake(); - expect(account.removeAccessor).to.exist; - expect(account.removeAccessor).to.be.a('function'); - expect(account.removeAccessor.length).to.be.equal(1); - expect(account.removeAccessor.name).to.be.equal('removeAccessor'); - }); + it('`removeAccessor` should be a function', () => { + const account = Account.fake(); + expect(account.removeAccessor).to.exist; + expect(account.removeAccessor).to.be.a('function'); + expect(account.removeAccessor.length).to.be.equal(1); + expect(account.removeAccessor.name).to.be.equal('removeAccessor'); + }); - it('should be able to remove existing accessor', () => { - const account = Account.fake(); - const exist = account.accessors.toObject(); - const accessor = account.accessors[0]; + it('should be able to remove existing accessor', () => { + const account = Account.fake(); + const exist = account.accessors.toObject(); + const accessor = account.accessors[0]; - account.removeAccessor(accessor.phone); - const current = account.accessors.toObject(); + account.removeAccessor(accessor.phone); + const current = account.accessors.toObject(); - expect(account.accessors).to.exist; - expect(current.length < exist.length).to.true; - expect(current).to.not.be.eql(exist); - }); + expect(account.accessors).to.exist; + expect(current.length < exist.length).to.true; + expect(current).to.not.be.eql(exist); }); +}); - describe('Hooks', () => { - describe('beforePost', () => { - let ensureLocation; - - const jurisdiction = Jurisdiction.fake(); - jurisdiction.location = randomPoint(); - - const account = Account.fake(); - account.jurisdiction = jurisdiction; +describe('Account Hooks', () => { + describe('beforePost', () => { + let ensureLocation; - beforeEach(() => { - ensureLocation = sinon.spy(account, 'ensureLocation'); - }); + const jurisdiction = Jurisdiction.fake(); + jurisdiction.location = randomPoint(); - afterEach(() => { - sinon.restore(); - }); + const account = Account.fake(); + account.jurisdiction = jurisdiction; - it('should be able to ensure location from jurisdiction', done => { - account.beforePost((error, updated) => { - // assert account - const { location } = updated; - expect(location).to.exist; - expect(location.type).to.exist; - expect(location.type).to.be.a('string'); - expect(location.type).to.be.equal(TYPE_POINT); - expect(location.coordinates).to.exist; - expect(location.coordinates).to.be.an('array'); - expect(location.coordinates).to.have.length(2); - - // assert methods call - expect(ensureLocation).to.have.been.called; - expect(ensureLocation).to.have.been.calledOnce; - - done(); - }); - }); + beforeEach(() => { + ensureLocation = sinon.spy(account, 'ensureLocation'); }); - }); - describe('Statics', () => { - it('should expose model name as constant', () => { - expect(Account.MODEL_NAME).to.exist; - expect(Account.MODEL_NAME).to.be.equal('Account'); + afterEach(() => { + sinon.restore(); }); - it('should expose autopulate as options', () => { - expect(Account.OPTION_AUTOPOPULATE).to.exist; - expect(Account.OPTION_AUTOPOPULATE).to.be.eql({ - select: { - number: 1, - identity: 1, - name: 1, - phone: 1, - email: 1, - locale: 1, - }, - maxDepth: 1, + it('should be able to ensure location from jurisdiction', done => { + account.beforePost((error, updated) => { + // assert account + const { location } = updated; + expect(location).to.exist; + expect(location.type).to.exist; + expect(location.type).to.be.a('string'); + expect(location.type).to.be.equal(TYPE_POINT); + expect(location.coordinates).to.exist; + expect(location.coordinates).to.be.an('array'); + expect(location.coordinates).to.have.length(2); + + // assert methods call + expect(ensureLocation).to.have.been.called; + expect(ensureLocation).to.have.been.calledOnce; + + done(); }); }); + }); +}); + +describe('Account Statics', () => { + it('should expose model name as constant', () => { + expect(Account.MODEL_NAME).to.exist; + expect(Account.MODEL_NAME).to.be.equal('Account'); + }); - it('should expose field select option', () => { - expect(Account.OPTION_SELECT).to.exist; - expect(Account.OPTION_SELECT).to.be.eql({ + it('should expose autopulate as options', () => { + expect(Account.OPTION_AUTOPOPULATE).to.exist; + expect(Account.OPTION_AUTOPOPULATE).to.be.eql({ + select: { number: 1, identity: 1, name: 1, phone: 1, email: 1, locale: 1, - }); + }, + maxDepth: 1, + }); + }); + + it('should expose field select option', () => { + expect(Account.OPTION_SELECT).to.exist; + expect(Account.OPTION_SELECT).to.be.eql({ + number: 1, + identity: 1, + name: 1, + phone: 1, + email: 1, + locale: 1, }); }); }); diff --git a/test/unit/account.schema.spec.js b/test/unit/account.schema.spec.js index c825069..035fd55 100644 --- a/test/unit/account.schema.spec.js +++ b/test/unit/account.schema.spec.js @@ -6,515 +6,462 @@ import Account from '../../src/account.model'; const DEFAULT_LOCALE = getString('DEFAULT_LOCALE', 'en'); -describe('Account', () => { - describe('Schema', () => { - it('should have jurisdiction field', () => { - const jurisdiction = Account.schema.path('jurisdiction'); - - expect(jurisdiction).to.exist; - expect(jurisdiction).to.be.an.instanceof(SchemaTypes.ObjectId); - expect(jurisdiction.instance).to.be.equal('ObjectID'); - expect(jurisdiction).to.be.an('object'); - expect(jurisdiction.options.type).to.be.a('function'); - expect(jurisdiction.options.type.name).to.be.equal('ObjectId'); - expect(jurisdiction.options.ref).to.be.equal(Jurisdiction.MODEL_NAME); - expect(jurisdiction.options.index).to.be.true; - expect(jurisdiction.options.exists).to.exist.and.be.an('object'); - expect(jurisdiction.options.autopopulate).to.exist.and.be.an('object'); +describe('Account Schema', () => { + it('should have jurisdiction field', () => { + const jurisdiction = Account.path('jurisdiction'); + + expect(jurisdiction).to.exist; + expect(jurisdiction).to.be.an.instanceof(SchemaTypes.ObjectId); + expect(jurisdiction.instance).to.be.equal('ObjectID'); + expect(jurisdiction).to.be.an('object'); + expect(jurisdiction.options.type).to.be.a('function'); + expect(jurisdiction.options.type.name).to.be.equal('ObjectId'); + expect(jurisdiction.options.ref).to.be.equal(Jurisdiction.MODEL_NAME); + expect(jurisdiction.options.index).to.be.true; + expect(jurisdiction.options.exists).to.exist.and.be.an('object'); + expect(jurisdiction.options.autopopulate).to.exist.and.be.an('object'); + }); + + it('should have category field', () => { + const category = Account.path('category'); + + expect(category).to.exist; + expect(category).to.be.an.instanceof(SchemaTypes.String); + expect(category.instance).to.be.equal('String'); + expect(category).to.be.an('object'); + expect(category.options.type).to.be.a('function'); + expect(category.options.type.name).to.be.equal('String'); + expect(category.options.trim).to.be.true; + expect(category.options.index).to.be.true; + expect(category.options.searchable).to.be.true; + expect(category.options.fake).to.exist; + }); + + it('should have number field', () => { + const number = Account.path('number'); + + expect(number).to.exist; + expect(number).to.be.an.instanceof(SchemaTypes.String); + expect(number.instance).to.be.equal('String'); + expect(number).to.be.an('object'); + expect(number.options.type).to.be.a('function'); + expect(number.options.type.name).to.be.equal('String'); + expect(number.options.required).to.be.true; + expect(number.options.uppercase).to.be.true; + expect(number.options.trim).to.be.true; + expect(number.options.index).to.be.true; + expect(number.options.searchable).to.be.true; + expect(number.options.fake).to.exist; + }); + + it('should have identity field', () => { + const identity = Account.path('identity'); + + expect(identity).to.exist; + expect(identity).to.be.an.instanceof(SchemaTypes.String); + expect(identity.instance).to.be.equal('String'); + expect(identity).to.be.an('object'); + expect(identity.options.type).to.be.a('function'); + expect(identity.options.type.name).to.be.equal('String'); + expect(identity.options.uppercase).to.be.true; + expect(identity.options.trim).to.be.true; + expect(identity.options.index).to.be.true; + expect(identity.options.searchable).to.be.true; + expect(identity.options.fake).to.exist; + }); + + it('should have name field', () => { + const name = Account.path('name'); + + expect(name).to.exist; + expect(name).to.be.an.instanceof(SchemaTypes.String); + expect(name.instance).to.be.equal('String'); + expect(name).to.be.an('object'); + expect(name.options.type).to.be.a('function'); + expect(name.options.type.name).to.be.equal('String'); + expect(name.options.required).to.be.true; + expect(name.options.trim).to.be.true; + expect(name.options.index).to.be.true; + expect(name.options.searchable).to.be.true; + expect(name.options.fake).to.exist; + }); + + it('should have phone field', () => { + const phone = Account.path('phone'); + + expect(phone).to.exist; + expect(phone).to.be.an.instanceof(SchemaTypes.String); + expect(phone.instance).to.be.equal('String'); + expect(phone).to.be.an('object'); + expect(phone.options.type).to.be.a('function'); + expect(phone.options.type.name).to.be.equal('String'); + expect(phone.options.required).to.be.true; + expect(phone.options.trim).to.be.true; + expect(phone.options.index).to.be.true; + expect(phone.options.searchable).to.be.true; + expect(phone.options.fake).to.exist; + }); + + it('should have email field', () => { + const email = Account.path('email'); + + expect(email).to.exist; + expect(email).to.be.an.instanceof(SchemaTypes.String); + expect(email.instance).to.be.equal('String'); + expect(email).to.be.an('object'); + expect(email.options.type).to.be.a('function'); + expect(email.options.type.name).to.be.equal('String'); + expect(email.options.lowercase).to.be.true; + expect(email.options.trim).to.be.true; + expect(email.options.index).to.be.true; + expect(email.options.searchable).to.be.true; + expect(email.options.fake).to.exist; + }); + + it('should have neighborhood field', () => { + const neighborhood = Account.path('neighborhood'); + + expect(neighborhood).to.exist; + expect(neighborhood).to.be.an.instanceof(SchemaTypes.String); + expect(neighborhood.instance).to.be.equal('String'); + expect(neighborhood).to.be.an('object'); + expect(neighborhood.options.type).to.be.a('function'); + expect(neighborhood.options.type.name).to.be.equal('String'); + expect(neighborhood.options.trim).to.be.true; + expect(neighborhood.options.index).to.be.true; + expect(neighborhood.options.searchable).to.be.true; + expect(neighborhood.options.fake).to.exist; + }); + + it('should have address field', () => { + const address = Account.path('address'); + + expect(address).to.exist; + expect(address).to.be.an.instanceof(SchemaTypes.String); + expect(address.instance).to.be.equal('String'); + expect(address).to.be.an('object'); + expect(address.options.type).to.be.a('function'); + expect(address.options.type.name).to.be.equal('String'); + expect(address.options.trim).to.be.true; + expect(address.options.index).to.be.true; + expect(address.options.searchable).to.be.true; + expect(address.options.fake).to.exist; + }); + + it('should have locale field', () => { + const locale = Account.path('locale'); + + expect(locale).to.exist; + expect(locale).to.be.an.instanceof(SchemaTypes.String); + expect(locale.instance).to.be.equal('String'); + expect(locale).to.be.an('object'); + expect(locale.options.type).to.be.a('function'); + expect(locale.options.type.name).to.be.equal('String'); + expect(locale.options.default).to.exist; + expect(locale.options.trim).to.be.true; + expect(locale.options.index).to.be.true; + expect(locale.options.searchable).to.be.true; + expect(locale.options.fake).to.exist; + expect(locale.options.default).to.be.equal(DEFAULT_LOCALE); + }); + + describe('location', () => { + it('should be an embedded subdocument', () => { + const location = Account.path('location'); + const type = Account.path('location.type'); + const coordinates = Account.path('location.coordinates'); + + expect(location).to.exist; + expect(type).to.be.instanceof(SchemaTypes.String); + expect(coordinates).to.be.instanceof(SchemaTypes.Array); }); - it('should have category field', () => { - const category = Account.schema.path('category'); - - expect(category).to.exist; - expect(category).to.be.an.instanceof(SchemaTypes.String); - expect(category.instance).to.be.equal('String'); - expect(category).to.be.an('object'); - expect(category.options.type).to.be.a('function'); - expect(category.options.type.name).to.be.equal('String'); - expect(category.options.trim).to.be.true; - expect(category.options.index).to.be.true; - expect(category.options.searchable).to.be.true; - expect(category.options.fake).to.exist; + it('should have GeoJSON type field', () => { + const location = Account.path('location'); + const type = Account.path('location.type'); + + expect(location).to.exist; + expect(type).to.be.instanceof(SchemaTypes.String); + expect(type.options).to.exist; + expect(type.options).to.be.an('object'); + expect(type.options.type).to.exist.and.be.a('function'); + expect(type.options.default).to.exist.and.be.equal('Point'); + expect(type.options.set).to.exist.and.be.a('function'); + }); + + it('should have GeoJSON coordinates field', () => { + const location = Account.path('location'); + const coordinates = Account.path('location.coordinates'); + + expect(location).to.exist; + expect(coordinates).to.be.instanceof(SchemaTypes.Array); + expect(coordinates.options).to.exist; + expect(coordinates.options).to.be.an('object'); + expect(coordinates.options.type).to.exist.and.be.a('function'); + expect(coordinates.options.default).to.be.undefined; + }); + }); + + describe('bills', () => { + it('should be an array of embedded subdocument', () => { + const bills = Account.path('bills'); + + expect(bills).to.exist; + expect(bills).to.be.instanceof(SchemaTypes.Array); }); it('should have number field', () => { - const number = Account.schema.path('number'); + const number = Account.path('bills.number'); expect(number).to.exist; - expect(number).to.be.an.instanceof(SchemaTypes.String); - expect(number.instance).to.be.equal('String'); - expect(number).to.be.an('object'); - expect(number.options.type).to.be.a('function'); + expect(number).to.be.instanceof(SchemaTypes.String); + expect(number.options).to.exist; + expect(number.options).to.be.an('object'); + expect(number.options.type).to.exist.and.be.a('function'); expect(number.options.type.name).to.be.equal('String'); - expect(number.options.required).to.be.true; - expect(number.options.uppercase).to.be.true; expect(number.options.trim).to.be.true; - expect(number.options.index).to.be.true; - expect(number.options.searchable).to.be.true; - expect(number.options.fake).to.exist; + expect(number.options.uppercase).to.be.true; }); - it('should have identity field', () => { - const identity = Account.schema.path('identity'); - - expect(identity).to.exist; - expect(identity).to.be.an.instanceof(SchemaTypes.String); - expect(identity.instance).to.be.equal('String'); - expect(identity).to.be.an('object'); - expect(identity.options.type).to.be.a('function'); - expect(identity.options.type.name).to.be.equal('String'); - expect(identity.options.uppercase).to.be.true; - expect(identity.options.trim).to.be.true; - expect(identity.options.index).to.be.true; - expect(identity.options.searchable).to.be.true; - expect(identity.options.fake).to.exist; - }); + describe('period', () => { + it('should be an embedded subdocument', () => { + const period = Account.path('bills.period'); - it('should have name field', () => { - const name = Account.schema.path('name'); - - expect(name).to.exist; - expect(name).to.be.an.instanceof(SchemaTypes.String); - expect(name.instance).to.be.equal('String'); - expect(name).to.be.an('object'); - expect(name.options.type).to.be.a('function'); - expect(name.options.type.name).to.be.equal('String'); - expect(name.options.required).to.be.true; - expect(name.options.trim).to.be.true; - expect(name.options.index).to.be.true; - expect(name.options.searchable).to.be.true; - expect(name.options.fake).to.exist; - }); + expect(period).to.exist; + expect(period).to.be.instanceof(SchemaTypes.Embedded); + }); - it('should have phone field', () => { - const phone = Account.schema.path('phone'); - - expect(phone).to.exist; - expect(phone).to.be.an.instanceof(SchemaTypes.String); - expect(phone.instance).to.be.equal('String'); - expect(phone).to.be.an('object'); - expect(phone.options.type).to.be.a('function'); - expect(phone.options.type.name).to.be.equal('String'); - expect(phone.options.required).to.be.true; - expect(phone.options.trim).to.be.true; - expect(phone.options.index).to.be.true; - expect(phone.options.searchable).to.be.true; - expect(phone.options.fake).to.exist; - }); + it('should have name', () => { + const name = Account.path('bills.period.name'); - it('should have email field', () => { - const email = Account.schema.path('email'); - - expect(email).to.exist; - expect(email).to.be.an.instanceof(SchemaTypes.String); - expect(email.instance).to.be.equal('String'); - expect(email).to.be.an('object'); - expect(email.options.type).to.be.a('function'); - expect(email.options.type.name).to.be.equal('String'); - expect(email.options.lowercase).to.be.true; - expect(email.options.trim).to.be.true; - expect(email.options.index).to.be.true; - expect(email.options.searchable).to.be.true; - expect(email.options.fake).to.exist; - }); + expect(name).to.exist; + expect(name).to.be.instanceof(SchemaTypes.String); + expect(name.options).to.exist; + expect(name.options).to.be.an('object'); + expect(name.options.type).to.exist.and.be.a('function'); + expect(name.options.type.name).to.be.equal('String'); + expect(name.options.trim).to.be.true; + }); - it('should have neighborhood field', () => { - const neighborhood = Account.schema.path('neighborhood'); - - expect(neighborhood).to.exist; - expect(neighborhood).to.be.an.instanceof(SchemaTypes.String); - expect(neighborhood.instance).to.be.equal('String'); - expect(neighborhood).to.be.an('object'); - expect(neighborhood.options.type).to.be.a('function'); - expect(neighborhood.options.type.name).to.be.equal('String'); - expect(neighborhood.options.trim).to.be.true; - expect(neighborhood.options.index).to.be.true; - expect(neighborhood.options.searchable).to.be.true; - expect(neighborhood.options.fake).to.exist; - }); + it('should have bill date', () => { + const billedAt = Account.path('bills.period.billedAt'); - it('should have address field', () => { - const address = Account.schema.path('address'); - - expect(address).to.exist; - expect(address).to.be.an.instanceof(SchemaTypes.String); - expect(address.instance).to.be.equal('String'); - expect(address).to.be.an('object'); - expect(address.options.type).to.be.a('function'); - expect(address.options.type.name).to.be.equal('String'); - expect(address.options.trim).to.be.true; - expect(address.options.index).to.be.true; - expect(address.options.searchable).to.be.true; - expect(address.options.fake).to.exist; - }); + expect(billedAt).to.exist; + expect(billedAt).to.be.instanceof(SchemaTypes.Date); + expect(billedAt.options).to.exist; + expect(billedAt.options).to.be.an('object'); + expect(billedAt.options.type).to.exist.and.be.a('function'); + expect(billedAt.options.type.name).to.be.equal('Date'); + expect(billedAt.options.fake).to.exist; + }); - it('should have locale field', () => { - const locale = Account.schema.path('locale'); - - expect(locale).to.exist; - expect(locale).to.be.an.instanceof(SchemaTypes.String); - expect(locale.instance).to.be.equal('String'); - expect(locale).to.be.an('object'); - expect(locale.options.type).to.be.a('function'); - expect(locale.options.type.name).to.be.equal('String'); - expect(locale.options.default).to.exist; - expect(locale.options.trim).to.be.true; - expect(locale.options.index).to.be.true; - expect(locale.options.searchable).to.be.true; - expect(locale.options.fake).to.exist; - expect(locale.options.default).to.be.equal(DEFAULT_LOCALE); - }); + it('should have start date', () => { + const startedAt = Account.path('bills.period.startedAt'); - describe('location', () => { - it('should be an embedded subdocument', () => { - const { location } = Account.schema.tree; - const { instance } = Account.schema.paths.location; - const { tree } = Account.schema.paths.location.schema; - - expect(instance).to.be.equal('Embedded'); - expect(location).to.exist; - expect(location).to.be.an('object'); - expect(tree).to.exist; - expect(tree.type).to.exist; - expect(tree.coordinates).to.exist; + expect(startedAt).to.exist; + expect(startedAt).to.be.instanceof(SchemaTypes.Date); + expect(startedAt.options).to.exist; + expect(startedAt.options).to.be.an('object'); + expect(startedAt.options.type).to.exist.and.be.a('function'); + expect(startedAt.options.type.name).to.be.equal('Date'); + expect(startedAt.options.fake).to.exist; }); - it('should have GeoJSON type field', () => { - const { schema } = Account.schema.paths.location; - const { type } = schema.tree; - const { instance } = schema.paths.type; - - expect(instance).to.be.equal('String'); - expect(type).to.exist; - expect(type).to.be.an('object'); - expect(type.type).to.be.a('function'); - expect(type.type.name).to.be.equal('String'); - expect(type.default).to.exist; + it('should have end date', () => { + const endedAt = Account.path('bills.period.endedAt'); + + expect(endedAt).to.exist; + expect(endedAt).to.be.instanceof(SchemaTypes.Date); + expect(endedAt.options).to.exist; + expect(endedAt.options).to.be.an('object'); + expect(endedAt.options.type).to.exist.and.be.a('function'); + expect(endedAt.options.type.name).to.be.equal('Date'); + expect(endedAt.options.fake).to.exist; }); - it.skip('should have GeoJSON coordinates field', () => { - const { schema } = Account.schema.paths.location; - const { coordinates } = schema.tree; - const { instance } = schema.paths.coordinates; + it('should have due date', () => { + const duedAt = Account.path('bills.period.duedAt'); - expect(instance).to.be.equal('Array'); - expect(coordinates).to.exist; - expect(coordinates).to.be.an('object'); - expect(coordinates.type[0]).to.be.a('function'); - expect(coordinates.type[0].name).to.be.equal('Number'); + expect(duedAt).to.exist; + expect(duedAt).to.be.instanceof(SchemaTypes.Date); + expect(duedAt.options).to.exist; + expect(duedAt.options).to.be.an('object'); + expect(duedAt.options.type).to.exist.and.be.a('function'); + expect(duedAt.options.type.name).to.be.equal('Date'); + expect(duedAt.options.fake).to.exist; }); }); - describe('bills', () => { - it('should be an array of embedded subdocument', () => { - const { bills } = Account.schema.tree; - const { instance } = Account.schema.paths.bills; - const { tree } = Account.schema.tree.bills[0]; - - expect(instance).to.be.equal('Array'); - expect(bills).to.exist; - expect(tree).to.exist; - expect(tree.number).to.exist; - expect(tree.period).to.exist; - expect(tree.balance).to.exist; - expect(tree.items).to.exist; - expect(tree.notes).to.exist; + describe('balance', () => { + it('should be an embedded subdocument', () => { + const balance = Account.path('bills.balance'); + + expect(balance).to.exist; + expect(balance).to.be.instanceof(SchemaTypes.Embedded); }); - it('should have number field', () => { - const { number } = Account.schema.tree.bills[0].tree; - const { instance } = Account.schema.paths.bills.schema.paths.number; - - expect(instance).to.be.equal('String'); - expect(number).to.exist; - expect(number).to.be.an('object'); - expect(number.type).to.be.a('function'); - expect(number.type.name).to.be.equal('String'); - expect(number.trim).to.be.true; - expect(number.uppercase).to.be.true; + it('should have outstand balance', () => { + const outstand = Account.path('bills.balance.outstand'); + + expect(outstand).to.exist; + expect(outstand).to.be.instanceof(SchemaTypes.Number); + expect(outstand.options).to.exist; + expect(outstand.options).to.be.an('object'); + expect(outstand.options.type).to.exist.and.be.a('function'); + expect(outstand.options.type.name).to.be.equal('Number'); + expect(outstand.options.fake).to.exist; }); - describe('period', () => { - it('should be an embedded subdocument', () => { - const { period } = Account.schema.tree.bills[0].tree; - const { instance } = Account.schema.paths.bills.schema.paths.period; - const { tree } = Account.schema.tree.bills[0].tree.period; - - expect(instance).to.be.equal('Embedded'); - expect(period).to.exist; - expect(period).to.be.an('object'); - expect(tree).to.exist; - expect(tree.name).to.exist; - expect(tree.startedAt).to.exist; - expect(tree.endedAt).to.exist; - expect(tree.duedAt).to.exist; - }); + it('should have open balance', () => { + const open = Account.path('bills.balance.open'); - it('should have name', () => { - const { name } = Account.schema.tree.bills[0].tree.period.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.period.schema.paths.name; + expect(open).to.exist; + expect(open).to.be.instanceof(SchemaTypes.Number); + expect(open.options).to.exist; + expect(open.options).to.be.an('object'); + expect(open.options.type).to.exist.and.be.a('function'); + expect(open.options.type.name).to.be.equal('Number'); + expect(open.options.fake).to.exist; + }); - expect(instance).to.be.equal('String'); - expect(name).to.exist; - expect(name).to.be.an('object'); - expect(name.trim).to.exist; - expect(name.required).to.not.exist; - expect(name.index).to.not.exist; - }); + it('should have period charges', () => { + const charges = Account.path('bills.balance.charges'); - it('should have bill date', () => { - const { billedAt } = Account.schema.tree.bills[0].tree.period.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.period.schema.paths.billedAt; - - expect(instance).to.be.equal('Date'); - expect(billedAt).to.exist; - expect(billedAt).to.be.an('object'); - expect(billedAt.required).to.not.exist; - expect(billedAt.index).to.not.exist; - }); + expect(charges).to.exist; + expect(charges).to.be.instanceof(SchemaTypes.Number); + expect(charges.options).to.exist; + expect(charges.options).to.be.an('object'); + expect(charges.options.type).to.exist.and.be.a('function'); + expect(charges.options.type.name).to.be.equal('Number'); + expect(charges.options.fake).to.exist; + }); - it('should have start date', () => { - const { startedAt } = Account.schema.tree.bills[0].tree.period.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.period.schema.paths.startedAt; - - expect(instance).to.be.equal('Date'); - expect(startedAt).to.exist; - expect(startedAt).to.be.an('object'); - expect(startedAt.required).to.not.exist; - expect(startedAt.index).to.not.exist; - }); + it('should have close balance', () => { + const close = Account.path('bills.balance.close'); - it('should have end date', () => { - const { endedAt } = Account.schema.tree.bills[0].tree.period.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.period.schema.paths.endedAt; - - expect(instance).to.be.equal('Date'); - expect(endedAt).to.exist; - expect(endedAt).to.be.an('object'); - expect(endedAt.required).to.not.exist; - expect(endedAt.index).to.not.exist; - }); + expect(close).to.exist; + expect(close).to.be.instanceof(SchemaTypes.Number); + expect(close.options).to.exist; + expect(close.options).to.be.an('object'); + expect(close.options.type).to.exist.and.be.a('function'); + expect(close.options.type.name).to.be.equal('Number'); + expect(close.options.fake).to.exist; + }); - it('should have due date', () => { - const { duedAt } = Account.schema.tree.bills[0].tree.period.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.period.schema.paths.duedAt; - - expect(instance).to.be.equal('Date'); - expect(duedAt).to.exist; - expect(duedAt).to.be.an('object'); - expect(duedAt.required).to.not.exist; - expect(duedAt.index).to.not.exist; - }); + it('should have debt balance', () => { + const debt = Account.path('bills.balance.debt'); + + expect(debt).to.exist; + expect(debt).to.be.instanceof(SchemaTypes.Number); + expect(debt.options).to.exist; + expect(debt.options).to.be.an('object'); + expect(debt.options.type).to.exist.and.be.a('function'); + expect(debt.options.type.name).to.be.equal('Number'); + expect(debt.options.fake).to.exist; }); + }); - describe('balance', () => { - it('should be an embedded subdocument', () => { - const { balance } = Account.schema.tree.bills[0].tree; - const { instance } = Account.schema.paths.bills.schema.paths.balance; - const { tree } = Account.schema.tree.bills[0].tree.balance; - - expect(instance).to.be.equal('Embedded'); - expect(balance).to.exist; - expect(balance).to.be.an('object'); - expect(tree).to.exist; - expect(tree.outstand).to.exist; - expect(tree.open).to.exist; - expect(tree.charges).to.exist; - expect(tree.close).to.exist; - }); + describe('items', () => { + it('should an array of embedded subdocument', () => { + const items = Account.path('bills.items'); - it('should have outstand balance', () => { - const { outstand } = Account.schema.tree.bills[0].tree.balance.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.balance.schema.paths.outstand; - - expect(instance).to.be.equal('Number'); - expect(outstand).to.exist; - expect(outstand).to.be.an('object'); - expect(outstand.required).to.not.exist; - expect(outstand.index).to.not.exist; - }); + expect(items).to.exist; + expect(items).to.be.instanceof(SchemaTypes.Array); + }); - it('should have open balance', () => { - const { open } = Account.schema.tree.bills[0].tree.balance.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.balance.schema.paths.open; - - expect(instance).to.be.equal('Number'); - expect(open).to.exist; - expect(open).to.be.an('object'); - expect(open.required).to.not.exist; - expect(open.index).to.not.exist; - }); + describe('item', () => { + it('should have name field', () => { + const name = Account.path('bills.items.name'); - it('should have period charges', () => { - const { charges } = Account.schema.tree.bills[0].tree.balance.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.balance.schema.paths.charges; - - expect(instance).to.be.equal('Number'); - expect(charges).to.exist; - expect(charges).to.be.an('object'); - expect(charges.required).to.not.exist; - expect(charges.index).to.not.exist; + expect(name).to.exist; + expect(name).to.be.instanceof(SchemaTypes.String); + expect(name.options).to.exist; + expect(name.options).to.be.an('object'); + expect(name.options.type).to.exist.and.be.a('function'); + expect(name.options.type.name).to.be.equal('String'); + expect(name.options.trim).to.be.true; + expect(name.options.fake).to.exist; }); - it('should have close balance', () => { - const { close } = Account.schema.tree.bills[0].tree.balance.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.balance.schema.paths.close; - - expect(instance).to.be.equal('Number'); - expect(close).to.exist; - expect(close).to.be.an('object'); - expect(close.required).to.not.exist; - expect(close.index).to.not.exist; - }); + it('should have quantity field', () => { + const quantity = Account.path('bills.items.quantity'); - it('should have debt balance', () => { - const { debt } = Account.schema.tree.bills[0].tree.balance.tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.balance.schema.paths.debt; - - expect(instance).to.be.equal('Number'); - expect(debt).to.exist; - expect(debt).to.be.an('object'); - expect(debt.required).to.not.exist; - expect(debt.index).to.not.exist; + expect(quantity).to.exist; + expect(quantity).to.be.instanceof(SchemaTypes.Number); + expect(quantity.options).to.exist; + expect(quantity.options).to.be.an('object'); + expect(quantity.options.type).to.exist.and.be.a('function'); + expect(quantity.options.type.name).to.be.equal('Number'); + expect(quantity.options.fake).to.exist; }); - }); - describe('items', () => { - it('should an array of embedded subdocument', () => { - const { items } = Account.schema.tree.bills[0].tree; - const { instance } = Account.schema.paths.bills.schema.paths.items; - const { tree } = Account.schema.tree.bills[0].tree.items[0]; - - expect(instance).to.be.equal('Array'); - expect(items).to.exist; - expect(items[0]).to.be.an('object'); - expect(tree).to.exist; - expect(tree.name).to.exist; - expect(tree.quantity).to.exist; - expect(tree.price).to.exist; - expect(tree.unit).to.exist; + it('should have price field', () => { + const price = Account.path('bills.items.price'); + + expect(price).to.exist; + expect(price).to.be.instanceof(SchemaTypes.Number); + expect(price.options).to.exist; + expect(price.options).to.be.an('object'); + expect(price.options.type).to.exist.and.be.a('function'); + expect(price.options.type.name).to.be.equal('Number'); + expect(price.options.fake).to.exist; }); - describe('item', () => { - it('should have name field', () => { - const { name } = Account.schema.tree.bills[0].tree.items[0].tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.items.schema.paths.name; - - expect(instance).to.be.equal('String'); - expect(name).to.exist; - expect(name).to.be.an('object'); - expect(name.trim).to.be.true; - expect(name.required).to.not.exist; - expect(name.index).to.not.exist; - }); - - it('should have quantity field', () => { - const { - quantity, - } = Account.schema.tree.bills[0].tree.items[0].tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.items.schema.paths.quantity; - - expect(instance).to.be.equal('Number'); - expect(quantity).to.exist; - expect(quantity).to.be.an('object'); - expect(quantity.required).to.not.exist; - expect(quantity.index).to.not.exist; - }); - - it('should have price field', () => { - const { price } = Account.schema.tree.bills[0].tree.items[0].tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.items.schema.paths.price; - - expect(instance).to.be.equal('Number'); - expect(price).to.exist; - expect(price).to.be.an('object'); - expect(price.required).to.not.exist; - expect(price.index).to.not.exist; - }); - - it('should have unit field', () => { - const { unit } = Account.schema.tree.bills[0].tree.items[0].tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.items.schema.paths.unit; - - expect(instance).to.be.equal('String'); - expect(unit).to.exist; - expect(unit).to.be.an('object'); - expect(unit.trim).to.be.true; - expect(unit.required).to.not.exist; - expect(unit.index).to.not.exist; - }); - - it('should have time field', () => { - const { time } = Account.schema.tree.bills[0].tree.items[0].tree; - const { - instance, - } = Account.schema.paths.bills.schema.paths.items.schema.paths.time; - - expect(instance).to.be.equal('Date'); - expect(time).to.exist; - expect(time).to.be.an('object'); - }); + it('should have unit field', () => { + const unit = Account.path('bills.items.unit'); + + expect(unit).to.exist; + expect(unit).to.be.instanceof(SchemaTypes.String); + expect(unit.options).to.exist; + expect(unit.options).to.be.an('object'); + expect(unit.options.type).to.exist.and.be.a('function'); + expect(unit.options.type.name).to.be.equal('String'); }); - }); - it('should have currency field', () => { - const { currency } = Account.schema.tree.bills[0].tree; - const { instance } = Account.schema.paths.bills.schema.paths.currency; - - expect(instance).to.be.equal('String'); - expect(currency).to.exist; - expect(currency).to.be.an('object'); - expect(currency.type).to.be.a('function'); - expect(currency.type.name).to.be.equal('String'); - expect(currency.trim).to.be.true; - expect(currency.uppercase).to.be.true; + it('should have time field', () => { + const time = Account.path('bills.items.time'); + + expect(time).to.exist; + expect(time).to.be.instanceof(SchemaTypes.Date); + expect(time.options).to.exist; + expect(time.options).to.be.an('object'); + expect(time.options.type).to.exist.and.be.a('function'); + expect(time.options.type.name).to.be.equal('Date'); + expect(time.options.fake).to.exist; + }); }); + }); - it('should have notes field', () => { - const { notes } = Account.schema.tree.bills[0].tree; - const { instance } = Account.schema.paths.bills.schema.paths.notes; + it('should have currency field', () => { + const currency = Account.path('bills.currency'); + + expect(currency).to.exist; + expect(currency).to.be.instanceof(SchemaTypes.String); + expect(currency.options).to.exist; + expect(currency.options).to.be.an('object'); + expect(currency.options.type).to.exist.and.be.a('function'); + expect(currency.options.type.name).to.be.equal('String'); + expect(currency.options.trim).to.be.true; + expect(currency.options.uppercase).to.be.true; + expect(currency.options.fake).to.exist; + }); - expect(instance).to.be.equal('String'); - expect(notes).to.exist; - expect(notes).to.be.an('object'); - expect(notes.type).to.be.a('function'); - expect(notes.type.name).to.be.equal('String'); - expect(notes.trim).to.be.true; - }); + it('should have notes field', () => { + const notes = Account.path('bills.notes'); + + expect(notes).to.exist; + expect(notes).to.be.instanceof(SchemaTypes.String); + expect(notes.options).to.exist; + expect(notes.options).to.be.an('object'); + expect(notes.options.type).to.exist.and.be.a('function'); + expect(notes.options.type.name).to.be.equal('String'); + expect(notes.options.trim).to.be.true; + expect(notes.options.fake).to.exist; }); }); it('should have fetchedAt field', () => { - const fetchedAt = Account.schema.path('fetchedAt'); + const fetchedAt = Account.path('fetchedAt'); expect(fetchedAt).to.exist; expect(fetchedAt).to.be.an.instanceof(SchemaTypes.Date); diff --git a/test/unit/index.spec.js b/test/unit/index.spec.js index 2399401..72e9020 100644 --- a/test/unit/index.spec.js +++ b/test/unit/index.spec.js @@ -1,19 +1,17 @@ import { expect } from '@lykmapipo/mongoose-test-helpers'; import account from '../../src'; -describe('account', () => { - it('should be exported', () => { - expect(account).to.exist; - expect(account).to.be.a('function'); - expect(account.name).to.be.equal('account'); - expect(account.length).to.be.equal(1); - }); +it('Account should be exported', () => { + expect(account).to.exist; + expect(account).to.be.a('function'); + expect(account.name).to.be.equal('account'); + expect(account.length).to.be.equal(1); +}); - it('should be export Account', () => { - const { Account } = account({ - fetchAccount: (identity, fetchedAt, done) => done(null, {}), - }); - expect(Account).to.exist; - expect(Account.fetchAccount).to.exist; +it('Account should be export Account', () => { + const { Account } = account({ + fetchAccount: (identity, fetchedAt, done) => done(null, {}), }); + expect(Account).to.exist; + expect(Account.fetchAccount).to.exist; });