How can I write angular test case for EventListener? - unit-testing

caps.component.ts:->
if (Acc.accOpen()) {
this.keyboardShowListener = Keyboard.addListener('keyboardDidShow', () => {
this.ngZone.run(() => {
this.isKeyboardVisible = true;
})
});
}
how can I write unit test case for this?

Related

Pinia - How to mock a specific action to test a store

Using pinia, I am not able to test what seems to be a simple scenario.
Store
const useStore = defineStore('store', () => {
const id = ref('id');
const forceId = ref('forceId');
function shouldReturnForceId() {
return false;
}
function getId() {
if (shouldReturnForceId()) {
return forceId.value;
}
return id.value;
}
return {
id,
forceId,
shouldReturnForceId,
getId,
};
});
Helper
const createTestStore = (stubActions: boolean) => {
const pinia = createTestingPinia({
stubActions,
});
setActivePinia(pinia);
const store = useStore();
return store;
};
Test
describe('Pinia Test', () => {
describe('getId', () => {
// DOES NOT WORK - stubAction to false and mocking shouldReturnForceId
it('should return forceId if shouldReturnForceId is true', () => {
const store = createTestStore(false);
store.shouldReturnForceId = jest.fn(() => true);
expect(store.getId()).toEqual('forceId');
});
// DOES NOT WORK - stubAction to true and mocking shouldReturnForceId
it('should return forceId if shouldReturnForceId is true', () => {
const store = createTestStore(true);
store.shouldReturnForceId = jest.fn(() => true);
expect(store.getId()).toEqual('forceId');
});
// WORKS
it('should return id if shouldReturnForceId is false', () => {
const store = createTestStore(false);
expect(store.getId()).toEqual('id');
});
});
});
Question
How is it possible to test a store by only mocking one action among several?
Reading the documentation I get that we can stubActions: true, which will mock all actions, but this is not what I need.

how to write unit test under this situation?

how to write unit test under this situation?
Vue Documents like this:
function alwaysEnabled() {
return false;
}
const actionDisabledFunctions = {
[CASE_ACTIONS.FLAG_CASE]: alwaysEnabled,
};
how do I write unit test inside describe?
describe('actionDisabledFunctions[CASE_ACTIONS.FLAG_CASE]', () => {
it('', () => {
});
});

Unable to write unit test for ngRx Store selector with Observable

This is the first time I am working on Jasmine unit test framework and I am trying to write unit test for the following lines of code in a component in Angular.
this.subscriptions.push(
this.store.select(authSelectors.getAuthError).subscribe((error) =\> {
if (error) {
this.loading = false;
this.utility.showErrorMessage(error);
if (this.loginForm) {
this.loginForm.reset();
} } }) );
Below is the code that I tried
describe('ngOnInit testing', () =\> {
it('Validate form', () =\> {
let mockError = new Observable\<any\>();
const storespy = jasmine.createSpyObj("Store",\['select'\]);
storespy.select.and.callFake((error: string) =\> {
switch(error) {
case authSelectors.getAuthError.toString(): return mockError;
default: return;
} });
if(mockError != null) { expect(utilityServiceSpy.showErrorMessage).toHaveBeenCalled();
} }) })
But this test case is failing with error:
Error: Expected spy UtilityService.showErrorMessage to have been called.
Am I doing anything wrong here?

Jest testing - cannot clear mocks

I have this tests in my nextjs app:
jest.mock('axios');
describe('Contacts', () => {
afterEach(() => {
axios.get.mockClear();
});
it('first test', async () => {
axios.get.mockImplementation((url: string) => {
if (url.includes('?page=1')) {
return Promise.resolve(ContactsFirstPageResponse);
} else {
return Promise.resolve(ContactsSecondPageResponse);
}
});
// ...
});
it('second test', async () => {
axios.get.mockImplementation(() => Promise.resolve(ContactsEmptyResponse));
// ...
});
});
First test passed, but second received response from ContactsFirstPageResponse
In addition I added auto clearing mocks on jest config:
const nextJest = require('next/jest');
const createJestConfig = nextJest({
dir: './',
});
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');
const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths),
resetMocks: true,
restoreMocks: true,
};
module.exports = createJestConfig(customJestConfig);
Please for help.

Unit Testing Angular Material 2 dialogs

While writing unit tests for a function that handles a angular material 2 dialog using the example code from material 2 i run into problems.
I'm a Jasmine newbie but I didn't had problems to write unit test before.
I have to test the result of the afterClose function but i can't get the handle to dialogRef.
Could it be a problem how the material2 dialog API is engineered?
let dialogRef = this.dialog.open(ExtractPageDialog, {
width: this.EXPORT_DIALOG_WIDTH,
data: {
document: this.document
}
});
dialogRef.afterClosed().subscribe((result: any) => {
if (result) {
let fileId = this.document.fileId;
this.docProvider.extractPage(this.document.fileId, result.fromPage, result.toPage).subscribe(() => {
() => { //totest },
(error) => { //totest }
});
} else {
//totest
}
});
DOCS:
https://material.angular.io/components/component/dialog
one solution could be to split the subscribed function in multiple functions and test them
dialogRef.afterClosed().subscribe(this.functionName);
functionName(result: any) {
if (result) {
let fileId = this.document.fileId;
this.docProvider.extractPage(this.document.fileId, result.fromPage, result.toPage).subscribe(() => {
() => { //totest },
(error) => { //totest }
});
} else {
//totest
}
}
}