MOBILE-3320 tests: Fix tests

main
Noel De Martin 2020-10-22 12:34:00 +02:00
parent 7f2751bb4f
commit 3647bc628a
5 changed files with 36 additions and 13 deletions

View File

@ -12,13 +12,24 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import { createComponent, prepareComponentTest } from '@/tests/utils'; import { createComponent, createMock, prepareComponentTest } from '@/tests/utils';
import { AppComponent } from '@app/app.component'; import { AppComponent } from '@app/app.component';
import { CoreLangProvider } from '@services/lang';
import { CoreEvents, CoreEventsProvider } from '@services/events';
describe('App component', () => { describe('App component', () => {
beforeEach(() => prepareComponentTest(AppComponent)); let langProvider: CoreLangProvider;
beforeEach(() => {
langProvider = createMock<CoreLangProvider>(['clearCustomStrings']);
CoreEvents.setInstance(new CoreEventsProvider());
prepareComponentTest(AppComponent, [
{ provide: CoreLangProvider, useValue: langProvider },
]);
});
it('should render', () => { it('should render', () => {
const fixture = createComponent(AppComponent); const fixture = createComponent(AppComponent);
@ -27,4 +38,13 @@ describe('App component', () => {
expect(fixture.nativeElement.querySelector('ion-router-outlet')).toBeTruthy(); expect(fixture.nativeElement.querySelector('ion-router-outlet')).toBeTruthy();
}); });
it('clears custom strings on logout', async () => {
const fixture = createComponent(AppComponent);
fixture.componentInstance.ngOnInit();
CoreEvents.instance.trigger(CoreEventsProvider.LOGOUT);
expect(langProvider.clearCustomStrings).toHaveBeenCalled();
});
}); });

View File

@ -38,13 +38,13 @@ describe('CoreLogin Init Page', () => {
expect(fixture.nativeElement.querySelector('ion-spinner')).toBeTruthy(); expect(fixture.nativeElement.querySelector('ion-spinner')).toBeTruthy();
}); });
it('navigates to site page after loading', async () => { it('navigates to sites page after loading', async () => {
const fixture = createComponent(CoreLoginInitPage); const fixture = createComponent(CoreLoginInitPage);
fixture.componentInstance.ngOnInit(); fixture.componentInstance.ngOnInit();
await CoreInit.instance.ready(); await CoreInit.instance.ready();
expect(mocks.router.navigate).toHaveBeenCalledWith(['/login/site']); expect(mocks.navController.navigateRoot).toHaveBeenCalledWith('/login/sites');
}); });
}); });

View File

@ -56,10 +56,12 @@ export class CoreLogger {
* @return Instance. * @return Instance.
*/ */
static getInstance(className: string): CoreLogger { static getInstance(className: string): CoreLogger {
// Disable log on production. // Disable log on production and testing.
if (CoreConstants.BUILD.isProduction || CoreConstants.BUILD.isTesting) {
if (CoreConstants.BUILD.isProduction) { if (CoreConstants.BUILD.isProduction) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.warn('Log is disabled in production app'); console.warn('Log is disabled in production app');
}
// eslint-disable-next-line @typescript-eslint/no-empty-function // eslint-disable-next-line @typescript-eslint/no-empty-function
const muted = () => {}; const muted = () => {};

View File

@ -14,7 +14,7 @@
import { CUSTOM_ELEMENTS_SCHEMA, Type } from '@angular/core'; import { CUSTOM_ELEMENTS_SCHEMA, Type } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Router } from '@angular/router'; import { NavController } from '@ionic/angular';
import { CoreSingletonClass } from '@app/classes/singletons-factory'; import { CoreSingletonClass } from '@app/classes/singletons-factory';
export interface ComponentTestMocks { export interface ComponentTestMocks {
@ -22,7 +22,7 @@ export interface ComponentTestMocks {
}; };
export interface PageTestMocks extends ComponentTestMocks { export interface PageTestMocks extends ComponentTestMocks {
router: Router; navController: NavController;
} }
export function createMock<T>(methods: string[] = [], properties: Record<string, unknown> = {}): T { export function createMock<T>(methods: string[] = [], properties: Record<string, unknown> = {}): T {
@ -57,12 +57,12 @@ export async function prepareComponentTest<T>(component: Type<T>, providers: unk
export async function preparePageTest<T>(component: Type<T>, providers: unknown[] = []): Promise<PageTestMocks> { export async function preparePageTest<T>(component: Type<T>, providers: unknown[] = []): Promise<PageTestMocks> {
const mocks = { const mocks = {
router: createMock<Router>(['navigate']), navController: createMock<NavController>(['navigateRoot']),
}; };
const componentTestMocks = await prepareComponentTest(component, [ const componentTestMocks = await prepareComponentTest(component, [
...providers, ...providers,
{ provide: Router, useValue: mocks.router }, { provide: NavController, useValue: mocks.navController },
]); ]);
return { return {

View File

@ -69,8 +69,9 @@ declare global {
}; };
BUILD: { BUILD: {
environment: string;
isProduction: boolean; isProduction: boolean;
isTesting: boolean;
isDevelopment: boolean;
lastCommitHash: string; lastCommitHash: string;
compilationTime: number; compilationTime: number;
}; };