Browse Source

test: enable and improve ToasterService specs

pull/3698/head
Arman Ozak 6 years ago
parent
commit
38db01c78e
  1. 155
      npm/ng-packs/packages/theme-shared/src/lib/tests/toaster.service.spec.ts

155
npm/ng-packs/packages/theme-shared/src/lib/tests/toaster.service.spec.ts

@ -1,87 +1,122 @@
import { CoreModule } from '@abp/ng.core'; import { CoreModule } from '@abp/ng.core';
import { Component } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterTestingModule } from '@angular/router/testing'; import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
import { NgxsModule } from '@ngxs/store'; import { NgxsModule } from '@ngxs/store';
import { timer } from 'rxjs';
import { ToastContainerComponent } from '../components/toast-container/toast-container.component';
import { ToastComponent } from '../components/toast/toast.component';
import { ToasterService } from '../services/toaster.service'; import { ToasterService } from '../services/toaster.service';
import { ThemeSharedModule } from '../theme-shared.module';
import { OAuthService } from 'angular-oauth2-oidc'; @NgModule({
exports: [ToastContainerComponent],
@Component({ entryComponents: [ToastContainerComponent],
selector: 'abp-dummy', declarations: [ToastContainerComponent, ToastComponent],
template: ` imports: [CoreModule.forTest()],
<abp-toast-container></abp-toast-container>
`,
}) })
class DummyComponent { export class MockModule {}
constructor(public toaster: ToasterService) {}
}
describe('ToasterService', () => { describe('ToasterService', () => {
let spectator: Spectator<DummyComponent>; let spectator: SpectatorService<ToasterService>;
let service: ToasterService; let service: ToasterService;
const createComponent = createComponentFactory({ const createService = createServiceFactory({
component: DummyComponent, service: ToasterService,
imports: [CoreModule, ThemeSharedModule.forRoot(), NgxsModule.forRoot(), RouterTestingModule], imports: [NgxsModule.forRoot(), CoreModule.forTest(), MockModule],
mocks: [OAuthService],
}); });
beforeEach(() => { beforeEach(() => {
spectator = createComponent(); spectator = createService();
service = spectator.get(ToasterService); service = spectator.service;
});
afterEach(() => {
clearElements();
}); });
test.skip('should display an error toast', () => { test('should display a toast', async () => {
service.error('test', 'title'); service.show('MESSAGE', 'TITLE');
spectator.detectChanges(); await timer(0).toPromise();
service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(spectator.query('div.toast')).toBeTruthy(); expect(selectToasterElement('.fa-exclamation-circle')).toBeTruthy();
expect(spectator.query('.toast-icon i')).toHaveClass('fa-times-circle'); expect(selectToasterContent('.toast-title')).toBe('TITLE');
expect(spectator.query('div.toast-title')).toHaveText('title'); expect(selectToasterContent('.toast-message')).toBe('MESSAGE');
expect(spectator.query('p.toast-message')).toHaveText('test');
}); });
test.skip('should display a warning toast', () => { test.each`
service.warn('test', 'title'); type | selector | icon
spectator.detectChanges(); ${'info'} | ${'.toast-info'} | ${'.fa-info-circle'}
expect(spectator.query('.toast-icon i')).toHaveClass('fa-exclamation-triangle'); ${'success'} | ${'.toast-success'} | ${'.fa-check-circle'}
${'warn'} | ${'.toast-warning'} | ${'.fa-exclamation-triangle'}
${'error'} | ${'.toast-error'} | ${'.fa-times-circle'}
`('should display $type toast', async ({ type, selector, icon }) => {
service[type]('MESSAGE', 'TITLE');
await timer(0).toPromise();
service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(selectToasterContent('.toast-title')).toBe('TITLE');
expect(selectToasterContent('.toast-message')).toBe('MESSAGE');
expect(selectToasterElement()).toBe(document.querySelector(selector));
expect(selectToasterElement(icon)).toBeTruthy();
}); });
test.skip('should display a success toast', () => { test('should display multiple toasts', async () => {
service.success('test', 'title'); service.show('MESSAGE_1', 'TITLE_1');
spectator.detectChanges(); service.show('MESSAGE_2', 'TITLE_2');
expect(spectator.query('.toast-icon i')).toHaveClass('fa-check-circle');
await timer(0).toPromise();
service['containerComponentRef'].changeDetectorRef.detectChanges();
const titles = document.querySelectorAll('.toast-title');
expect(titles.length).toBe(2);
const messages = document.querySelectorAll('.toast-message');
expect(messages.length).toBe(2);
}); });
test.skip('should display an info toast', () => { test('should remove a toast when remove is called', async () => {
service.info('test', 'title'); service.show('MESSAGE');
spectator.detectChanges(); service.remove(0);
expect(spectator.query('.toast-icon i')).toHaveClass('fa-info-circle');
await timer(0).toPromise();
service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(selectToasterElement()).toBeNull();
}); });
test.skip('should display multiple toasts', () => { test('should remove toasts when clear is called', async () => {
service.info('detail1', 'summary1'); service.show('MESSAGE');
service.info('detail2', 'summary2'); service.clear();
spectator.detectChanges(); await timer(0).toPromise();
expect(spectator.queryAll('div.toast-title').map(node => node.textContent.trim())).toEqual([ service['containerComponentRef'].changeDetectorRef.detectChanges();
'summary1',
'summary2', expect(selectToasterElement()).toBeNull();
]);
expect(spectator.queryAll('p.toast-message').map(node => node.textContent.trim())).toEqual([
'detail1',
'detail2',
]);
}); });
test.skip('should remove the opened toasts', () => { test('should remove toasts based on containerKey when clear is called with key', async () => {
service.info('test', 'title'); service.show('MESSAGE_1', 'TITLE_1', 'neutral', { containerKey: 'x' });
spectator.detectChanges(); service.show('MESSAGE_2', 'TITLE_2', 'neutral', { containerKey: 'y' });
expect(spectator.query('div.toast')).toBeTruthy(); service.clear('x');
service.clear(); await timer(0).toPromise();
spectator.detectChanges(); service['containerComponentRef'].changeDetectorRef.detectChanges();
expect(spectator.query('p-div.toast')).toBeFalsy();
expect(selectToasterElement('.fa-exclamation-circle')).toBeTruthy();
expect(selectToasterContent('.toast-title')).toBe('TITLE_2');
expect(selectToasterContent('.toast-message')).toBe('MESSAGE_2');
}); });
}); });
function clearElements(selector = '.toast') {
document.querySelectorAll(selector).forEach(element => element.parentNode.removeChild(element));
}
function selectToasterContent(selector = '.toast'): string {
return selectToasterElement(selector).textContent.trim();
}
function selectToasterElement<T extends HTMLElement>(selector = '.toast'): T {
return document.querySelector(selector);
}

Loading…
Cancel
Save