Guard TestBed
Custom test bed for testing guard.
Quick example
- function
- Class
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();
}));
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard);
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).
- function
- Class
describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' })
it('should ', tb((tools) => { // 👈 tb function used here
// ... expectations
}));
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard)
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
- function
- Class
describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, {} /* 👈 here */);
it('should ', tb(() => {
// ... expectations
}));
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard, {} /* 👈 here */);
it('should ', tb(() => {
// ... expectations
}));
});
type
Default : undefined
Specifies the type of guard being tested.
Required for guard functions and optional for guard classes.
Recommanded for guard classes that implement many interfaces to choose what guard mecanism will be performed by the tools.
Example :
- function
- Class
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' });
const tb = guardTestBed(AuthGuard, { type: 'CanMatch' });
imports
Default : []
Imports dependencies for the described guard.
Example :
- function
- Class
const tb = guardTestBed(AUTH_GUARD, {
type: 'CanActivate',
imports: [SharedModule, AuthModule],
});
const tb = guardTestBed(AuthGuard, {
imports: [SharedModule, AuthModule],
});
providers
Default : []
List of providers to be available during tests for the described guard.
Example :
- function
- Class
const tb = guardTestBed(AUTH_GUARD, {
type: 'CanActivate',
providers: [AppService, { provide: StoreService, useClass: MockStoreService }],
});
const tb = guardTestBed(AuthGuard, {
providers: [AppService, { provide: StoreService, useClass: MockStoreService }],
});
httpTesting
Default : false
Enables HttpTestingTools.
verifyHttp
Default : true
When enabled, each assertion will end by HttpTestingController.verify()
.
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
.
- function
- Class
describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' });
it('should ', tb((tools /* 👈 here */) => {
// ... expectations
}));
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard);
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.
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.
- function
- Class
describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' });
it('should ', tb((tools) => {
// ... expectations
}, {} /* 👈 here */));
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard);
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 :
- function
- Class
describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' })
.import(AppModule)
.import([SharedModule, ThirdPartyModule]);
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard)
.import(AppModule)
.import([SharedModule, ThirdPartyModule]);
});
provide(..)
Same as options providers but with chaining methods.
Example :
- function
- Class
describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' })
.provide(AppService)
.provide([StoreService, { provide: MY_TOKEN, useValue: mockValue }]);
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard)
.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.
- function
- Class
describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' })
.inject('auth', AuthService);
it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard)
.inject('auth', AuthService);
it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});
setup(..)
Setups extra action using the enhanced tools.
Works only for beforeEach
and afterEach
.
- function
- Class
describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate' }});
beforeEach(tb.setup(({ guard }) => {
guard.foo = true;
}));
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard);
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.
It has to be used into beforeEach(..)
and autoCompile must be set to false
.
- function
- Class
describe('AUTH_GUARD', () => {
const tb = guardTestBed(AUTH_GUARD, { type: 'CanActivate', autoCompile: false });
beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});
describe('AuthGuard', () => {
const tb = guardTestBed(AuthGuard, { autoCompile: false });
beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});