Aller au contenu principal

Guard TestBed

Custom test bed for testing guard.

Quick example

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' });

it('should not activate if logged', tb(({ challenge }) => {
const result1 = challenge();
expect(result1).toBeFalse();

const result2 = challenge.withInfo({ data: { isAllowed: true } });
expect(result2).toBeTrue();
}));
});

guardTestBed(..)

Creates a specific test bed for router.

It returns a function to be used to wrap it's callback and from which you access tools (check GuardTools).

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' })

it('should ', tb((tools) => { // 👈 tb function used here
// ... expectations
}));
});

tb function supports the jasmine DoneFn and async/await notation.

describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard);

it('should ', tb(async (tools) => {
// ... async expectations
}));

it('should ', tb((tools, done) => {
// ... expectations
done();
}));
});

TestBed Options

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, {} /* 👈 here */);

it('should ', tb(() => {
// ... expectations
}));
});

type

Default : undefined

Specifies the type of guard being tested.

info

Required for guard functions and optional for guard classes.

remarque

Recommanded for guard classes that implement many interfaces to choose what guard mecanism will be performed by the tools.

Example :

const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' });

imports

Default : []

Imports dependencies for the described guard.

Example :

const tb = guardTestBed(AUTH_GUARD, {
type: 'CanActivate',
imports: [SharedModule, AuthModule],
});

providers

Default : []

List of providers to be available during tests for the described guard.

Example :

const tb = guardTestBed(AUTH_GUARD, {
type: 'CanActivate',
providers: [AppService, { provide: StoreService, useClass: MockStoreService }],
});

httpTesting

Default : false

Enables HttpTestingTools.

verifyHttp

Default : true

When enabled, each assertion will end by HttpTestingController.verify().

attention

Works only if httpTesting is true, otherwise has no effect.

autoCompile

Default : true

Automatically compiles the custom test bed for each test.

checkCreate

Default : true

Automatically invokes the "should create" Angular test.

It checks if the provided described instance is truthy.

Assertion tools

The tb function provides GuardTools.

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' });

it('should ', tb((tools /* 👈 here */) => {
// ... expectations
}));
});

👉 GuardTools extends BaseTools and HttpTestingTools.

guard

The described guard.

Example :

it('should ', tb(({ guard }) => {
expect(guard.foo).toBeTrue();
}));

challenge

The main tool for testing guard.

It runs the described guard to check its output as it in its current context.

info

The return type of the challenge function is the same as the return type of the described guard.

Example :

it('should not pass', tb(({ challenge }) => {
const result = challenge();
expect(result).toBeFalse();
}));

You can add more routing context by providing extra routing information by running challenge.withInfo(..)

Example :

it('should pass if user agreed', tb(({ challenge }) => {
const result1 = challenge();
expect(result1).toBeFalse();

const result2 = challenge.withInfo({ queryParams: { agreed: true } });
expect(result2).toBeTrue();
}));

RoutingInfo

data

Default : {}

params

Default : {}

queryParams

Default : {}

currentState
nextState
component
route
segments

Assertion options

For specific test, you enable/disable options that override the test bed options.

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' });

it('should ', tb((tools) => {
// ... expectations
}, {} /* 👈 here */));
});

verifyHttp

Same as options verifyHttp but only for the current assertion.

GuardTestBed

import(..)

Same as options imports but with chaining methods.

Example :

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' })
.import(AppModule)
.import([SharedModule, ThirdPartyModule]);
});

provide(..)

Same as options providers but with chaining methods.

Example :

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' })
.provide(AppService)
.provide([StoreService, { provide: MY_TOKEN, useValue: mockValue }]);
});

inject(..)

Links an injected instance to a key and retrieve it into the enhanced tools by autocompletion.

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' })
.inject('auth', AuthService);

it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});

setup(..)

Setups extra action using the enhanced tools.

Works only for beforeEach and afterEach.

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' }});

beforeEach(tb.setup(({ guard }) => {
guard.foo = true;
}));
});

compile()

To be used when you need to do third party setups before compiling the custom test bed.

attention

It has to be used into beforeEach(..) and autoCompile must be set to false.

describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate', autoCompile: false });

beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});