Resolver TestBed
Custom test bed for testing Resolver.
Works for function (ResolverFn
) and class (that implements Resolver
) resolver.
Quick example
- function
- Class
describe('appResolver', () => {
const tb = resolverTestBed(appResolver());
it('should get the resolver data', tb(async ({ trigger }) => {
const result = await trigger();
expect(result.foo).toEqual('bar');
}));
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver);
it('should get the resolver data', tb(async ({ trigger }) => {
const result = await trigger();
expect(result.foo).toEqual('bar');
}));
});
resolverTestBed(..)
Creates a specific test bed for resolver.
It returns a function to be used to wrap it
's callback and from which you access tools (check ResolverTools).
- function
- Class
describe('appResolver', () => {
const tb = resolverTestBed(appResolver());
it('should ', tb((tools) => { // 👈 tb function used here
// ... expectations
}));
});
tb
function supports the jasmine DoneFn
and async/await notation.
describe('appResolver', () => {
const tb = resolverTestBed(appResolver());
it('should ', tb(async (tools) => {
// ... async expectations
}));
it('should ', tb((tools, done) => {
// ... expectations
done();
}));
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver);
it('should ', tb((tools) => { // 👈 tb function used here
// ... expectations
}));
});
tb
function supports the jasmine DoneFn
and async/await notation.
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver);
it('should ', tb(async (tools) => {
// ... async expectations
}));
it('should ', tb((tools, done) => {
// ... expectations
done();
}));
});
TestBed Options
- function
- Class
describe('appResolver', () => {
const tb = resolverTestBed(appResolver(), {} /* 👈 here */);
it('should ', tb(() => {
// ... expectations
}));
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver, {} /* 👈 here */);
it('should ', tb(() => {
// ... expectations
}));
});
imports
Default : []
Imports dependencies for the described resolver.
Example :
- function
- Class
const tb = resolverTestBed(appResolver(), {
imports: [SharedModule, MaterialModule],
});
const tb = resolverTestBed(AppResolver, {
imports: [SharedModule, MaterialModule],
});
providers
Default : []
List of providers to be available during tests for the described resolver.
Example :
- function
- Class
const tb = resolverTestBed(appResolver(), {
providers: [AuthService, { provide: StoreService, useClass: MockStoreService }],
});
const tb = resolverTestBed(AppResolver, {
providers: [AuthService, { provide: StoreService, useClass: MockStoreService }],
});
verifyHttp
Default : true
When enabled, each assertion will end by HttpTestingController.verify()
.
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 ResolverTools
.
- function
- Class
describe('appResolver', () => {
const tb = resolverTestBed(appResolver());
it('should ', tb((tools /* 👈 here */) => {
// ... expectations
}));
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver);
it('should ', tb((tools /* 👈 here */) => {
// ... expectations
}));
});
ResolverTools
extends BaseTools and HttpTestingTools.
resolver
The described resolver instance.
The instance is typed according to the passed resolver in resolverTestBed(..)
.
Example :
- function
- Class
it('should ', tb(({ resolver }) => {
resolver(mockRoute, mockState).subscribe(..);
}));
it('should ', tb(({ resolver }) => {
expect(resolver.prop).toEqual('foo');
}));
trigger
Tools to inspect outgoing requests and incoming responses.
The return type of the trigger
function is the same as the return type of the described resolver.
Example :
it('should get the resolver data', tb(async ({ trigger }) => {
const result = await trigger();
expect(result.foo).toEqual('bar');
}));
You can add more routing context by providing extra routing information by running trigger.withInfo(..)
Example :
it('should set custom header before send', tb(async ({ trigger }) => {
const result = await trigger.withInfo({ params: { id: 1 } });
expect(result.foo).toEqual('bar');
}));
Assertion options
For specific test, you enable/disable options that override the test bed options.
- function
- Class
describe('appResolver', () => {
const tb = resolverTestBed(appResolver());
it('should ', tb((tools) => {
// ... expectations
}, {} /* 👈 here */));
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver);
it('should ', tb((tools) => {
// ... expectations
}, {} /* 👈 here */));
});
verifyHttp
Same as options verifyHttp but only for the current assertion.
InterceptorTestBed
import(..)
Same as options imports but with chaining methods.
Example :
- function
- Class
describe('appResolver', () => {
const tb = resolverTestBed(appResolver())
.import(SharedModule)
.import([ThirdPartyModule, MaterialModule]);
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver)
.import(SharedModule)
.import([ThirdPartyModule, MaterialModule]);
});
provide(..)
Same as options providers but with chaining methods.
Example :
- function
- Class
describe('appResolver', () => {
const tb = resolverTestBed(appResolver())
.provide(NotifService)
.provide([StoreService, { provide: MY_TOKEN, useValue: mockValue }]);
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver)
.provide(NotifService)
.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('appResolver', () => {
const tb = resolverTestBed(appResolver())
.inject('auth', AuthService);
it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver)
.inject('auth', AuthService);
it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});
setup(..)
Setups extra action using the enhanced tools.
Works only for beforeEach
and afterEach
.
Example :
- function
- Class
describe('appResolver', () => {
const tb = resolverTestBed(appResolver())
inject('store', StoreService);
beforeEach(tb.setup(({ injected: { store } }) => {
store.foo = true;
}));
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver);
beforeEach(tb.setup(({ resolver }) => {
resolver.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('appResolver', () => {
const tb = resolverTestBed(appResolver(), { autoCompile: false });
beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});
describe('AppResolver', () => {
const tb = resolverTestBed(AppResolver, { autoCompile: false });
beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});
itShouldCreateResolver(..)
Create the "should create" for the described resolver.
To be used when there are no noticeable or relevant tests to be performed.
- function
- Class
describe('appResolver', () => {
itShouldCreateResolver(appResolver());
});
describe('AppResolver', () => {
itShouldCreateResolver(AppResolver);
});