2019-08-31 20:51:14 -07:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupTest } from 'ember-qunit';
|
|
|
|
import { NotificationEvent, NotificationType } from '@datahub/utils/constants/notifications';
|
|
|
|
import sinonTest from 'ember-sinon-qunit/test-support/test';
|
|
|
|
import { INotificationsTestContext } from '@datahub/utils/types/tests/notifications';
|
2020-08-26 15:44:50 -07:00
|
|
|
import { timeout } from 'ember-concurrency';
|
2019-08-31 20:51:14 -07:00
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
module('Unit | Service | notifications', function(hooks): void {
|
2019-08-31 20:51:14 -07:00
|
|
|
setupTest(hooks);
|
|
|
|
hooks.beforeEach(function(this: INotificationsTestContext) {
|
|
|
|
this.service = this.owner.lookup('service:notifications');
|
|
|
|
});
|
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
test('it exists', function(this: INotificationsTestContext, assert): void {
|
2019-08-31 20:51:14 -07:00
|
|
|
const { service } = this;
|
|
|
|
assert.ok(service, 'Expected Notifications to be a service');
|
|
|
|
|
|
|
|
assert.notOk(service.isShowingNotification, 'Expected isShowingNotification to be false');
|
|
|
|
assert.notOk(service.activeNotification, 'Expected service to not have an active notification');
|
|
|
|
assert.notOk(service.isBuffering, 'Expected service to not be buffering when not active notification exist');
|
|
|
|
});
|
|
|
|
|
|
|
|
sinonTest('Service dequeue invocation arguments', async function(
|
|
|
|
this: SinonTestContext & INotificationsTestContext,
|
|
|
|
assert
|
2020-08-26 15:44:50 -07:00
|
|
|
): Promise<void> {
|
|
|
|
assert.expect(2);
|
2019-08-31 20:51:14 -07:00
|
|
|
const { service } = this;
|
|
|
|
const stubbedDequeue = this.stub(service, 'asyncDequeue');
|
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
service.notify({ type: NotificationEvent.success, duration: 0 });
|
2019-08-31 20:51:14 -07:00
|
|
|
assert.ok(
|
|
|
|
stubbedDequeue.calledWith(service.notificationsQueue),
|
|
|
|
'Expected the dequeue method to be called with the services notifications queue'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(service.notificationsQueue.length, 1, 'Expected a notification to be placed in the queue');
|
|
|
|
|
|
|
|
await service.setCurrentNotificationTask.last;
|
|
|
|
});
|
|
|
|
|
|
|
|
sinonTest('Service state flags post notification', function(
|
|
|
|
this: SinonTestContext & INotificationsTestContext,
|
|
|
|
assert
|
2020-08-26 15:44:50 -07:00
|
|
|
): void {
|
2019-08-31 20:51:14 -07:00
|
|
|
const { service } = this;
|
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
service.notify({ type: NotificationEvent.success, duration: 0 });
|
2019-08-31 20:51:14 -07:00
|
|
|
|
|
|
|
assert.ok(service.isBuffering, 'Expected notifications service to be processing the queue');
|
|
|
|
assert.ok(
|
|
|
|
service.isShowingNotification,
|
|
|
|
'Expected the isShowing flag to be true when there is an active notification'
|
|
|
|
);
|
|
|
|
assert.ok(service.activeNotification, 'Expected the active notification flag to be true');
|
|
|
|
});
|
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
test('toast dismissal', function(this: INotificationsTestContext, assert): void {
|
2019-08-31 20:51:14 -07:00
|
|
|
const { service } = this;
|
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
service.notify({ type: NotificationEvent.success, duration: 0 });
|
2019-08-31 20:51:14 -07:00
|
|
|
|
|
|
|
service.dismissToast();
|
|
|
|
assert.notOk(service.isShowingNotification, 'Expected notification to be dismissed');
|
|
|
|
assert.notOk(service.isShowingToast, 'Expected toast notification to be falsy');
|
|
|
|
});
|
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
test('showing toast detail', async function(this: INotificationsTestContext, assert): Promise<void> {
|
2019-08-31 20:51:14 -07:00
|
|
|
assert.expect(3);
|
|
|
|
const { service } = this;
|
|
|
|
|
|
|
|
service.notify({
|
2020-08-26 15:44:50 -07:00
|
|
|
duration: 0,
|
2019-08-31 20:51:14 -07:00
|
|
|
type: NotificationEvent.success,
|
|
|
|
content:
|
|
|
|
'A long string of text for user notification that contains more than the required number of characters to be displayed in a toast'
|
|
|
|
});
|
|
|
|
|
2020-08-26 15:44:50 -07:00
|
|
|
// Expect the notification to still be rendered after timeout
|
|
|
|
// this matches the expected behavior in the UI where the user asynchronously clicks a button to show more detail
|
|
|
|
await timeout(0.25 * 1000);
|
|
|
|
|
2019-08-31 20:51:14 -07:00
|
|
|
service.showContentDetail();
|
2020-08-26 15:44:50 -07:00
|
|
|
|
|
|
|
await service.setCurrentNotificationTask.last;
|
|
|
|
|
|
|
|
assert.ok(service.isShowingNotification, 'Expected the notification flag to be truthy');
|
|
|
|
|
|
|
|
const { activeNotification = { type: null } } = service;
|
|
|
|
assert.equal(
|
|
|
|
activeNotification.type,
|
|
|
|
NotificationType.Modal,
|
|
|
|
'Expected the active notification to be a modal when showing content detail'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.notOk(service.isShowingToast, 'Expected the active toast notification indicator flag to be falsy');
|
2019-08-31 20:51:14 -07:00
|
|
|
});
|
|
|
|
});
|