Browse Source

test: enable and improve ConfirmationService specs

pull/3698/head
Arman Ozak 7 years ago
parent
commit
d7240ce148
  1. 125
      npm/ng-packs/packages/theme-shared/src/lib/tests/confirmation.service.spec.ts

125
npm/ng-packs/packages/theme-shared/src/lib/tests/confirmation.service.spec.ts

@ -1,74 +1,101 @@
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 { fakeAsync, tick } from '@angular/core/testing';
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest'; import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import { NgxsModule } from '@ngxs/store'; import { NgxsModule } from '@ngxs/store';
import { ConfirmationService } from '../services/confirmation.service'; import { timer } from 'rxjs';
import { ThemeSharedModule } from '../theme-shared.module'; import { take } from 'rxjs/operators';
import { OAuthModule, OAuthService } from 'angular-oauth2-oidc'; import { ConfirmationComponent } from '../components';
import { Confirmation } from '../models';
@Component({ import { ConfirmationService } from '../services';
selector: 'abp-dummy',
template: ` @NgModule({
<abp-confirmation></abp-confirmation> exports: [ConfirmationComponent],
`, entryComponents: [ConfirmationComponent],
declarations: [ConfirmationComponent],
imports: [CoreModule.forTest()],
}) })
class DummyComponent { export class MockModule {}
constructor(public confirmation: ConfirmationService) {}
}
describe('ConfirmationService', () => { describe('ConfirmationService', () => {
let spectator: Spectator<DummyComponent>; let spectator: SpectatorService<ConfirmationService>;
let service: ConfirmationService; let service: ConfirmationService;
const createComponent = createComponentFactory({ const createService = createServiceFactory({
component: DummyComponent, service: ConfirmationService,
imports: [CoreModule, ThemeSharedModule.forRoot(), NgxsModule.forRoot(), RouterTestingModule], imports: [NgxsModule.forRoot(), CoreModule.forTest(), MockModule],
mocks: [OAuthService],
}); });
beforeEach(() => { beforeEach(() => {
spectator = createComponent(); spectator = createService();
service = spectator.get(ConfirmationService); service = spectator.service;
});
afterEach(() => {
clearElements();
}); });
test.skip('should display a confirmation popup', () => { test('should display a confirmation popup', fakeAsync(() => {
service.info('test', 'title'); service.show('MESSAGE', 'TITLE');
spectator.detectChanges(); tick();
expect(spectator.query('div.confirmation .title')).toHaveText('title'); expect(selectConfirmationContent('.title')).toBe('TITLE');
expect(spectator.query('div.confirmation .message')).toHaveText('test'); expect(selectConfirmationContent('.message')).toBe('MESSAGE');
}));
test.each`
type | selector | icon
${'info'} | ${'.info'} | ${'.fa-info-circle'}
${'success'} | ${'.success'} | ${'.fa-check-circle'}
${'warn'} | ${'.warning'} | ${'.fa-exclamation-triangle'}
${'error'} | ${'.error'} | ${'.fa-times-circle'}
`('should display $type confirmation popup', async ({ type, selector, icon }) => {
service[type]('MESSAGE', 'TITLE');
await timer(0).toPromise();
expect(selectConfirmationContent('.title')).toBe('TITLE');
expect(selectConfirmationContent('.message')).toBe('MESSAGE');
expect(selectConfirmationElement(selector)).toBeTruthy();
expect(selectConfirmationElement(icon)).toBeTruthy();
}); });
test.skip('should close with ESC key', done => { test('should close with ESC key', done => {
service.info('test', 'title').subscribe(() => { service
setTimeout(() => { .info('', '')
spectator.detectComponentChanges(); .pipe(take(1))
expect(spectator.query('div.confirmation')).toBeFalsy(); .subscribe(status => {
expect(status).toBe(Confirmation.Status.dismiss);
done(); done();
}, 0); });
});
spectator.detectChanges(); const escape = new KeyboardEvent('keyup', { key: 'Escape' });
expect(spectator.query('div.confirmation')).toBeTruthy(); document.dispatchEvent(escape);
spectator.dispatchKeyboardEvent('div.confirmation', 'keyup', 'Escape');
}); });
test.skip('should close when click cancel button', done => { test('should close when click cancel button', async done => {
service.info('test', 'title', { yesText: 'Sure', cancelText: 'Exit' }).subscribe(() => { service.info('', '', { yesText: 'Sure', cancelText: 'Exit' }).subscribe(status => {
spectator.detectComponentChanges(); expect(status).toBe(Confirmation.Status.reject);
setTimeout(() => { done();
expect(spectator.query('div.confirmation')).toBeFalsy();
done();
}, 0);
}); });
spectator.detectChanges(); await timer(0).toPromise();
expect(spectator.query('div.confirmation')).toBeTruthy(); expect(selectConfirmationContent('button#cancel')).toBe('Exit');
expect(spectator.query('button#cancel')).toHaveText('Exit'); expect(selectConfirmationContent('button#confirm')).toBe('Sure');
expect(spectator.query('button#confirm')).toHaveText('Sure');
spectator.click('button#cancel'); selectConfirmationElement<HTMLButtonElement>('button#cancel').click();
}); });
}); });
function clearElements(selector = '.confirmation') {
document.querySelectorAll(selector).forEach(element => element.parentNode.removeChild(element));
}
function selectConfirmationContent(selector = '.confirmation'): string {
return selectConfirmationElement(selector).textContent.trim();
}
function selectConfirmationElement<T extends HTMLElement>(selector = '.confirmation'): T {
return document.querySelector(selector);
}

Loading…
Cancel
Save