|
| 1 | +// Copyright IBM Corp. 2017,2018. All Rights Reserved. |
| 2 | +// Node module: @loopback/repository |
| 3 | +// This file is licensed under the MIT License. |
| 4 | +// License text available at https://opensource.org/licenses/MIT |
| 5 | + |
| 6 | +import {expect} from '@loopback/testlab'; |
| 7 | +import { |
| 8 | + Entity, |
| 9 | + EntityNotFoundError, |
| 10 | + model, |
| 11 | + isEntityNotFoundError, |
| 12 | +} from '../../../'; |
| 13 | + |
| 14 | +describe('EntityNotFoundError', () => { |
| 15 | + it('inherits from Error correctly', () => { |
| 16 | + const err = givenAnErrorInstance(); |
| 17 | + expect(err).to.be.instanceof(EntityNotFoundError); |
| 18 | + expect(err).to.be.instanceof(Error); |
| 19 | + expect(err.stack) |
| 20 | + .to.be.String() |
| 21 | + .and.match((s: string) => s.indexOf(__filename) > -1); |
| 22 | + }); |
| 23 | + |
| 24 | + it('sets code to "ENTITY_NOT_FOUND"', () => { |
| 25 | + const err = givenAnErrorInstance(); |
| 26 | + expect(err.code).to.equal('ENTITY_NOT_FOUND'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('sets entity name and id properties', () => { |
| 30 | + const err = new EntityNotFoundError('Product', 'an-id'); |
| 31 | + expect(err).to.have.properties({ |
| 32 | + entityName: 'Product', |
| 33 | + entityId: 'an-id', |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('has a descriptive error message', () => { |
| 38 | + const err = new EntityNotFoundError('Product', 'an-id'); |
| 39 | + expect(err.message).to.match(/not found.*Product.*"an-id"/); |
| 40 | + }); |
| 41 | + |
| 42 | + it('infers entity name from entity class via name', () => { |
| 43 | + class Product extends Entity {} |
| 44 | + |
| 45 | + const err = new EntityNotFoundError(Product, 1); |
| 46 | + expect(err.entityName).to.equal('Product'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('infers entity name from entity class via modelName', () => { |
| 50 | + @model({name: 'CustomProduct'}) |
| 51 | + class Product extends Entity {} |
| 52 | + |
| 53 | + const err = new EntityNotFoundError(Product, 1); |
| 54 | + expect(err.entityName).to.equal('CustomProduct'); |
| 55 | + }); |
| 56 | + |
| 57 | + function givenAnErrorInstance() { |
| 58 | + return new EntityNotFoundError('Product', 1); |
| 59 | + } |
| 60 | +}); |
| 61 | + |
| 62 | +describe('isEntityNotFoundError', () => { |
| 63 | + it('returns true for an instance of EntityNotFoundError', () => { |
| 64 | + const error = new EntityNotFoundError('Product', 123); |
| 65 | + expect(isEntityNotFoundError(error)).to.be.true(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns false for an instance of Error', () => { |
| 69 | + const error = new Error('A generic error'); |
| 70 | + expect(isEntityNotFoundError(error)).to.be.false(); |
| 71 | + }); |
| 72 | +}); |
0 commit comments