From 58e7a78ee7e50b12af4973258896585349fd81b9 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Tue, 13 Oct 2020 19:15:29 +0200 Subject: [PATCH] MOBILE-3320 tests: Add base error tests --- package-lock.json | 6 +++ package.json | 1 + src/app/classes/error.test.ts | 73 +++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/app/classes/error.test.ts diff --git a/package-lock.json b/package-lock.json index 3e5419c59..25072b58c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8219,6 +8219,12 @@ "resolved": "https://registry.npmjs.org/extsprintf/-/extsprintf-1.4.0.tgz", "integrity": "sha1-4mifjzVvrWLMplo6kcXfX5VRaS8=" }, + "faker": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/faker/-/faker-5.1.0.tgz", + "integrity": "sha512-RrWKFSSA/aNLP0g3o2WW1Zez7/MnMr7xkiZmoCfAGZmdkDQZ6l2KtuXHN5XjdvpRjDl8+3vf+Rrtl06Z352+Mw==", + "dev": true + }, "fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", diff --git a/package.json b/package.json index 0027a939e..7c084682c 100644 --- a/package.json +++ b/package.json @@ -130,6 +130,7 @@ "eslint-plugin-jest": "^24.1.0", "eslint-plugin-jsdoc": "^30.6.3", "eslint-plugin-prefer-arrow": "^1.2.2", + "faker": "^5.1.0", "jest": "^26.5.0", "jest-preset-angular": "^8.3.1", "ts-jest": "^26.4.1", diff --git a/src/app/classes/error.test.ts b/src/app/classes/error.test.ts new file mode 100644 index 000000000..ceaf99365 --- /dev/null +++ b/src/app/classes/error.test.ts @@ -0,0 +1,73 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +import Faker from 'faker'; + +import { CoreError } from './error'; + +describe('CoreError', () => { + it('behaves like an error', () => { + // Arrange + const message = Faker.lorem.sentence(); + + let error: CoreError | null = null; + + // Act + try { + throw new CoreError(message); + } catch (e) { + error = e; + } + + // Assert + expect(error).not.toBeNull(); + expect(error).toBeInstanceOf(Error); + expect(error).toBeInstanceOf(CoreError); + expect(error!.name).toEqual('CoreError'); + expect(error!.message).toEqual(message); + expect(error!.stack).not.toBeNull(); + expect(error!.stack).toContain('src/app/classes/error.test.ts'); + }); + + it('can be subclassed', () => { + // Arrange + class CustomCoreError extends CoreError { + + constructor(m: string) { + super(`Custom message: ${m}`); + } + + } + + const message = Faker.lorem.sentence(); + + let error: CustomCoreError | null = null; + + // Act + try { + throw new CustomCoreError(message); + } catch (e) { + error = e; + } + + // Assert + expect(error).not.toBeNull(); + expect(error).toBeInstanceOf(Error); + expect(error).toBeInstanceOf(CoreError); + expect(error).toBeInstanceOf(CustomCoreError); + expect(error!.name).toEqual('CustomCoreError'); + expect(error!.message).toEqual(`Custom message: ${message}`); + expect(error!.stack).not.toBeNull(); + expect(error!.stack).toContain('src/app/classes/error.test.ts'); + }); +});