Skip to content
This repository has been archived by the owner on Jul 29, 2023. It is now read-only.

Commit

Permalink
test: add tests for module initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
chernodub committed Nov 9, 2022
1 parent 0935535 commit 01d5c1d
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions projects/ngx-lss/src/lib/ngx-local-storage.module.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Component, NgModule } from '@angular/core';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { Router, RouterModule } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';

import { NgxLocalStorageModule } from './ngx-local-storage.module';
import { NgxLocalStorageService } from './ngx-local-storage.service';

@Component({ template: '<router-outlet></router-outlet>' })
export class DummyRootComponent {}

@Component({ template: '' })
export class LazyDummyComponent {}

@NgModule({
declarations: [],
imports: [
NgxLocalStorageModule.forRoot(),
RouterModule.forChild([{ path: '', pathMatch: 'full', component: LazyDummyComponent }]),
],
})
export class LazyDummyModule {}

describe('NgxLocalStorageModule', () => {
describe('without `forRoot` call', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgxLocalStorageModule],
});
});

it('throws an error', () => {
expect(() => TestBed.inject(NgxLocalStorageService)).toThrow()
});
});

describe('with `forRoot` call', () => {
describe('when imported once', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgxLocalStorageModule.forRoot()],
});
});

it('injects service', () => {
expect(TestBed.inject(NgxLocalStorageService)).toBeTruthy();
});
});

describe('when imported more than once', () => {
let router: Router;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [DummyRootComponent],
imports: [
NgxLocalStorageModule.forRoot(),
RouterTestingModule.withRoutes([
{
path: '',
pathMatch: 'full',
loadChildren: () => Promise.resolve(LazyDummyModule),
},
]),
],
});

router = TestBed.inject(Router);
});

// Using `fakeAsync()` and `tick()` to wait for the router to initialize the lazy module
it('throws an error on module initialization', fakeAsync(() => {

expect(() => {
router.initialNavigation();
tick();
}).toThrowError();
}));
});
});
});

0 comments on commit 01d5c1d

Please sign in to comment.