Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wendell committed Nov 22, 2019
1 parent 2c00f31 commit 90f8fc4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
28 changes: 9 additions & 19 deletions components/message/nz-message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { Component, TemplateRef, ViewChild } from '@angular/core';
import { fakeAsync, inject, tick, ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { dispatchMouseEvent, NzConfigService } from 'ng-zorro-antd/core';
import { dispatchMouseEvent, NzConfig, NzConfigService, NZ_CONFIG } from 'ng-zorro-antd/core';

import { NZ_MESSAGE_CONFIG } from './nz-message-config';
import { NzMessageModule } from './nz-message.module';
import { NzMessageService } from './nz-message.service';

Expand All @@ -17,26 +16,29 @@ describe('NzMessage', () => {
let nzConfigService: NzConfigService;

beforeEach(fakeAsync(() => {
const MESSAGE_CONFIG: NzConfig['message'] = {
nzMaxStack: 2,
nzTop: 24
};

TestBed.configureTestingModule({
imports: [NzMessageModule, NoopAnimationsModule],
declarations: [NzTestMessageBasicComponent],
providers: [{ provide: NZ_MESSAGE_CONFIG, useValue: { nzMaxStack: 2, nzTop: 24 } }]
providers: [{ provide: NZ_CONFIG, useValue: { message: MESSAGE_CONFIG } }]
});

TestBed.compileComponents();
}));

beforeEach(inject([NzMessageService, OverlayContainer], (m: NzMessageService, oc: OverlayContainer) => {
messageService = m;
// @ts-ignore
nzConfigService = messageService._container.nzConfigService;
if (!overlayContainerElement) {
overlayContainerElement = oc.getContainerElement();
}
}));

beforeEach(inject([NzConfigService], (c: NzConfigService) => {
nzConfigService = c;
}));

afterEach(() => {
messageService.remove();
});
Expand Down Expand Up @@ -161,17 +163,6 @@ describe('NzMessage', () => {
expect(overlayContainerElement.textContent).not.toContain('EXISTS');
}));

/**
* @deprecated This test is going to be removed in 9.0.0
*/
it('should reset default config dynamically', fakeAsync(() => {
messageService.config({ nzDuration: 0 });
messageService.create('loading', 'EXISTS');
fixture.detectChanges();
tick(10000);
expect(overlayContainerElement.textContent).toContain('EXISTS');
}));

it('should reset default config from config service', fakeAsync(() => {
nzConfigService.set('message', { nzDuration: 0 });
messageService.create('loading', 'EXISTS');
Expand All @@ -181,7 +172,6 @@ describe('NzMessage', () => {
}));

it('should emit event when message close', fakeAsync(() => {
messageService.config({ nzDuration: 2000 });
const closeSpy = jasmine.createSpy('message closed');
const msg = messageService.create('loading', 'CLOSE');
const messageId = msg.messageId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const NZ_NOTIFICATION_DEFAULT_CONFIG: Required<NotificationConfig> = {
templateUrl: './nz-notification-container.component.html'
})
export class NzNotificationContainerComponent extends NzMessageContainerComponent {
config: Required<NotificationConfig> = { ...NZ_NOTIFICATION_DEFAULT_CONFIG };
config: Required<NotificationConfig>;
bottom: string | null;

/**
Expand Down
32 changes: 18 additions & 14 deletions components/notification/nz-notification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { Component, TemplateRef, ViewChild } from '@angular/core';
import { fakeAsync, inject, tick, ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { dispatchMouseEvent } from 'ng-zorro-antd/core';
import { dispatchMouseEvent, NzConfig, NzConfigService, NZ_CONFIG } from 'ng-zorro-antd/core';

import { NZ_NOTIFICATION_CONFIG } from './nz-notification-config';
import { NzNotificationModule } from './nz-notification.module';
import { NzNotificationService } from './nz-notification.service';

Expand All @@ -22,32 +21,39 @@ describe('NzNotification', () => {
let notificationService: NzNotificationService;
let overlayContainerElement: HTMLElement;
let fixture: ComponentFixture<DemoAppComponent>;
let nzConfigService: NzConfigService;

beforeEach(fakeAsync(() => {
const NOTIFICATION_CONFIG: NzConfig['notification'] = {
nzMaxStack: 2
};

TestBed.configureTestingModule({
imports: [NzNotificationModule, NoopAnimationsModule],
declarations: [DemoAppComponent],
providers: [{ provide: NZ_NOTIFICATION_CONFIG, useValue: { nzMaxStack: 2 } }] // Override default config
providers: [{ provide: NZ_CONFIG, useValue: { notification: NOTIFICATION_CONFIG } }]
});

TestBed.compileComponents();
}));

beforeEach(inject([NzNotificationService, OverlayContainer], (n: NzNotificationService, oc: OverlayContainer) => {
notificationService = n;
// @ts-ignore
nzConfigService = notificationService._container.nzConfigService;
if (!overlayContainerElement) {
overlayContainerElement = oc.getContainerElement();
}
}));

afterEach(() => {
notificationService.remove();
});

beforeEach(() => {
fixture = TestBed.createComponent(DemoAppComponent);
});

afterEach(() => {
notificationService.remove();
});

it('should open a message box with success', () => {
notificationService.success('test-title', 'SUCCESS');
fixture.detectChanges();
Expand Down Expand Up @@ -151,23 +157,21 @@ describe('NzNotification', () => {
tick(1000 + 10);
expect(overlayContainerElement.textContent).not.toContain('EXISTS');
}));

it('should reset default config dynamically', fakeAsync(() => {
notificationService.config({ nzDuration: 0 });
nzConfigService.set('notification', { nzDuration: 0 });
notificationService.create('', 'loading', 'EXISTS');
fixture.detectChanges();
tick(50000);
tick(10000);
expect(overlayContainerElement.textContent).toContain('EXISTS');
}));

it('should show with placement of topLeft', () => {
notificationService.config({ nzPlacement: 'topLeft' });
nzConfigService.set('notification', { nzPlacement: 'topLeft' });
notificationService.create('', '', 'EXISTS');
fixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('EXISTS');
expect(overlayContainerElement.querySelector('.ant-notification-topLeft')).not.toBeNull();
});

// Should support nzData as context.
it('should open a message box with template ref', () => {
notificationService.template(fixture.componentInstance.demoTemplateRef, { nzData: 'data' });
Expand Down Expand Up @@ -205,14 +209,14 @@ describe('NzNotification', () => {
}));

it('should support configurable nzTop & nzBottom', fakeAsync(() => {
notificationService.config({ nzTop: 48 });
nzConfigService.set('notification', { nzTop: 48 });
notificationService.create('', '', 'TEST TOP', { nzDuration: 3000 });
waitForNotificationToggling(fixture);
const notificationContainer = overlayContainerElement.querySelector('.ant-notification') as HTMLElement;
expect(notificationContainer.style.top).toBe('48px');
expect(notificationContainer.style.bottom).toBeFalsy();

notificationService.config({ nzPlacement: 'bottomLeft', nzBottom: '48px' });
nzConfigService.set('notification', { nzPlacement: 'bottomLeft', nzBottom: '48px' });
notificationService.create('', '', 'TEST BOTTOM');
waitForNotificationToggling(fixture);
expect(notificationContainer.style.top).toBeFalsy();
Expand Down

0 comments on commit 90f8fc4

Please sign in to comment.