2017-08-22 10:53:26 +03:00
import EmberObject from '@ember/object' ;
2015-06-19 00:56:18 +03:00
import sinon from 'sinon' ;
2016-01-18 18:37:14 +03:00
import { AjaxError , InvalidError } from 'ember-ajax/errors' ;
2016-06-14 14:46:24 +03:00
import { ServerUnreachableError } from 'ghost-admin/services/ajax' ;
2017-05-29 21:50:03 +03:00
import { describe , it } from 'mocha' ;
2017-08-22 10:53:26 +03:00
import { A as emberA } from '@ember/array' ;
2017-05-29 21:50:03 +03:00
import { expect } from 'chai' ;
2017-08-22 10:53:26 +03:00
import { run } from '@ember/runloop' ;
2017-05-29 21:50:03 +03:00
import { setupTest } from 'ember-mocha' ;
2015-06-19 00:56:18 +03:00
2022-05-27 13:52:39 +03:00
import { GENERIC _ERROR _MESSAGE } from 'ghost-admin/services/notifications' ;
2020-01-07 16:23:15 +03:00
// notifications service determines if a notification is a model instance by
// checking `notification.constructor.modelName === 'notification'`
const NotificationStub = EmberObject . extend ( ) ;
NotificationStub . modelName = 'notification' ;
2016-11-24 01:50:57 +03:00
describe ( 'Unit: Service: notifications' , function ( ) {
2019-05-13 15:43:53 +03:00
setupTest ( ) ;
2017-03-29 01:22:02 +03:00
2016-11-24 01:50:57 +03:00
beforeEach ( function ( ) {
2019-05-13 15:43:53 +03:00
this . owner . lookup ( 'service:notifications' ) . set ( 'content' , emberA ( ) ) ;
this . owner . lookup ( 'service:notifications' ) . set ( 'delayedNotifications' , emberA ( ) ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
it ( 'filters alerts/notifications' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
// wrapped in run-loop to enure alerts/notifications CPs are updated
run ( ( ) => {
notifications . showAlert ( 'Alert' ) ;
notifications . showNotification ( 'Notification' ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . length ) . to . equal ( 1 ) ;
expect ( notifications . alerts . firstObject . message ) . to . equal ( 'Alert' ) ;
2015-06-19 00:56:18 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . notifications . length ) . to . equal ( 1 ) ;
expect ( notifications . notifications . firstObject . message ) . to . equal ( 'Notification' ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
it ( '#handleNotification deals with DS.Notification notifications' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2020-01-07 16:23:15 +03:00
let notification = NotificationStub . create ( { message : '<h1>Test</h1>' , status : 'alert' } ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
notifications . handleNotification ( notification ) ;
2015-06-19 00:56:18 +03:00
2022-05-25 14:43:02 +03:00
notification = notifications . alerts [ 0 ] ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
// alerts received from the server should be marked html safe
2022-05-25 14:43:02 +03:00
expect ( notification . message ) . to . have . property ( 'toHTML' ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
it ( '#handleNotification defaults to notification if no status supplied' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2016-11-24 01:50:57 +03:00
notifications . handleNotification ( { message : 'Test' } , false ) ;
2015-06-19 00:56:18 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . content )
2016-11-24 01:50:57 +03:00
. to . deep . include ( { message : 'Test' , status : 'notification' } ) ;
} ) ;
2015-06-19 00:56:18 +03:00
2022-05-27 13:52:39 +03:00
it ( '#handleNotification shows generic error message when a word matches built-in error type' , function ( ) {
let notifications = this . owner . lookup ( 'service:notifications' ) ;
notifications . handleNotification ( { message : 'TypeError test' } ) ;
expect ( notifications . content [ 0 ] . message ) . to . equal ( GENERIC _ERROR _MESSAGE ) ;
notifications . clearAll ( ) ;
expect ( notifications . content . length ) . to . equal ( 0 ) ;
notifications . handleNotification ( { message : 'TypeError: Testing' } ) ;
expect ( notifications . content [ 0 ] . message ) . to . equal ( GENERIC _ERROR _MESSAGE ) ;
notifications . clearAll ( ) ;
notifications . handleNotification ( { message : 'Unknown error - TypeError, cannot save invite.' } ) ;
expect ( notifications . content [ 0 ] . message ) . to . equal ( GENERIC _ERROR _MESSAGE ) ;
} ) ;
2016-11-24 01:50:57 +03:00
it ( '#showAlert adds POJO alerts' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAlert ( 'Test Alert' , { type : 'error' } ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts )
2020-02-27 12:19:29 +03:00
. to . deep . include ( { message : 'Test Alert' , status : 'alert' , type : 'error' , key : undefined , actions : undefined , description : undefined , icon : undefined } ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
it ( '#showAlert adds delayed notifications' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showNotification ( 'Test Alert' , { type : 'error' , delayed : true } ) ;
2015-10-07 17:44:23 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . delayedNotifications )
2020-02-27 12:19:29 +03:00
. to . deep . include ( { message : 'Test Alert' , status : 'notification' , type : 'error' , key : undefined , actions : undefined , description : undefined , icon : undefined } ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
// in order to cater for complex keys that are suitable for i18n
// we split on the second period and treat the resulting base as
// the key for duplicate checking
2019-03-12 20:50:45 +03:00
it ( '#showAlert clears duplicates using keys' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAlert ( 'Kept' ) ;
notifications . showAlert ( 'Duplicate' , { key : 'duplicate.key.fail' } ) ;
} ) ;
2015-10-07 17:44:23 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . length ) . to . equal ( 2 ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAlert ( 'Duplicate with new message' , { key : 'duplicate.key.success' } ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . length ) . to . equal ( 2 ) ;
expect ( notifications . alerts . lastObject . message ) . to . equal ( 'Duplicate with new message' ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-06-19 00:56:18 +03:00
2019-03-12 20:50:45 +03:00
it ( '#showAlert clears duplicates using message text' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2019-03-12 20:50:45 +03:00
notifications . showAlert ( 'Not duplicate' ) ;
notifications . showAlert ( 'Duplicate' , { key : 'duplicate' } ) ;
notifications . showAlert ( 'Duplicate' ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . length ) . to . equal ( 2 ) ;
expect ( notifications . alerts . lastObject . key ) . to . not . exist ;
2019-03-12 20:50:45 +03:00
} ) ;
2016-11-24 01:50:57 +03:00
it ( '#showNotification adds POJO notifications' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showNotification ( 'Test Notification' , { type : 'success' } ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . notifications )
2020-02-27 12:19:29 +03:00
. to . deep . include ( { message : 'Test Notification' , status : 'notification' , type : 'success' , key : undefined , actions : undefined , description : undefined , icon : undefined } ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
it ( '#showNotification adds delayed notifications' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showNotification ( 'Test Notification' , { delayed : true } ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . delayedNotifications )
2020-02-27 12:19:29 +03:00
. to . deep . include ( { message : 'Test Notification' , status : 'notification' , type : undefined , key : undefined , actions : undefined , description : undefined , icon : undefined } ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
it ( '#showAPIError handles single json response error' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2017-11-04 01:59:39 +03:00
let error = new AjaxError ( { errors : [ { message : 'Single error' } ] } ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAPIError ( error ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
let [ alert ] = notifications . alerts ;
expect ( alert . message ) . to . equal ( 'Single error' ) ;
expect ( alert . status ) . to . equal ( 'alert' ) ;
expect ( alert . type ) . to . equal ( 'error' ) ;
expect ( alert . key ) . to . equal ( 'api-error' ) ;
2016-11-24 01:50:57 +03:00
} ) ;
it ( '#showAPIError handles multiple json response errors' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2017-11-04 01:59:39 +03:00
let error = new AjaxError ( { errors : [
2016-11-24 01:50:57 +03:00
{ title : 'First error' , message : 'First error message' } ,
{ title : 'Second error' , message : 'Second error message' }
2017-11-04 01:59:39 +03:00
] } ) ;
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAPIError ( error ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . length ) . to . equal ( 2 ) ;
let [ alert1 , alert2 ] = notifications . alerts ;
2020-02-27 12:19:29 +03:00
expect ( alert1 ) . to . deep . equal ( { message : 'First error message' , status : 'alert' , type : 'error' , key : 'api-error.first-error' , actions : undefined , description : undefined , icon : undefined } ) ;
expect ( alert2 ) . to . deep . equal ( { message : 'Second error message' , status : 'alert' , type : 'error' , key : 'api-error.second-error' , actions : undefined , description : undefined , icon : undefined } ) ;
2016-11-24 01:50:57 +03:00
} ) ;
it ( '#showAPIError displays default error text if response has no error/message' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2016-11-24 01:50:57 +03:00
let resp = false ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAPIError ( resp ) ;
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . content ) . to . deep . equal ( [
2020-02-27 12:19:29 +03:00
{ message : 'There was a problem on the server, please try again.' , status : 'alert' , type : 'error' , key : 'api-error' , actions : undefined , description : undefined , icon : undefined }
2016-11-24 01:50:57 +03:00
] ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
notifications . set ( 'content' , emberA ( ) ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAPIError ( resp , { defaultErrorText : 'Overridden default' } ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . content ) . to . deep . equal ( [
2020-02-27 12:19:29 +03:00
{ message : 'Overridden default' , status : 'alert' , type : 'error' , key : 'api-error' , actions : undefined , description : undefined , icon : undefined }
2016-11-24 01:50:57 +03:00
] ) ;
} ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
it ( '#showAPIError sets correct key when passed a base key' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAPIError ( 'Test' , { key : 'test.alert' } ) ;
2015-10-07 17:44:23 +03:00
} ) ;
2015-06-19 00:56:18 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . firstObject . key ) . to . equal ( 'api-error.test.alert' ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
it ( '#showAPIError sets correct key when not passed a key' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAPIError ( 'Test' ) ;
2015-10-07 17:44:23 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . firstObject . key ) . to . equal ( 'api-error' ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2016-06-14 14:46:24 +03:00
2016-11-24 01:50:57 +03:00
it ( '#showAPIError parses default ember-ajax errors correctly' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2016-11-24 01:50:57 +03:00
let error = new InvalidError ( ) ;
2016-06-14 14:46:24 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAPIError ( error ) ;
2016-06-14 14:46:24 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
let notification = notifications . alerts . firstObject ;
expect ( notification . message ) . to . equal ( 'Request was rejected because it was invalid' ) ;
expect ( notification . status ) . to . equal ( 'alert' ) ;
expect ( notification . type ) . to . equal ( 'error' ) ;
expect ( notification . key ) . to . equal ( 'api-error' ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2016-01-18 18:37:14 +03:00
2016-11-24 01:50:57 +03:00
it ( '#showAPIError parses custom ember-ajax errors correctly' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2016-11-24 01:50:57 +03:00
let error = new ServerUnreachableError ( ) ;
2016-01-18 18:37:14 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAPIError ( error ) ;
2016-01-18 18:37:14 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
let notification = notifications . alerts . firstObject ;
expect ( notification . message ) . to . equal ( 'Server was unreachable' ) ;
expect ( notification . status ) . to . equal ( 'alert' ) ;
expect ( notification . type ) . to . equal ( 'error' ) ;
expect ( notification . key ) . to . equal ( 'api-error' ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2019-03-06 14:45:47 +03:00
it ( '#showAPIError adds error context to message if available' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2019-03-06 14:45:47 +03:00
let error = new AjaxError ( { errors : [ {
message : 'Authorization Error.' ,
context : 'Please sign in.'
} ] } ) ;
run ( ( ) => {
notifications . showAPIError ( error ) ;
} ) ;
2022-05-25 14:43:02 +03:00
let [ alert ] = notifications . alerts ;
expect ( alert . message ) . to . equal ( 'Authorization Error. Please sign in.' ) ;
expect ( alert . status ) . to . equal ( 'alert' ) ;
expect ( alert . type ) . to . equal ( 'error' ) ;
expect ( alert . key ) . to . equal ( 'api-error' ) ;
2019-03-06 14:45:47 +03:00
} ) ;
2022-11-23 17:57:32 +03:00
it ( '#showAPIError does not add context to message if it duplicates the message' , function ( ) {
let notifications = this . owner . lookup ( 'service:notifications' ) ;
let error = new AjaxError ( { errors : [ {
message : 'Authorization Error.' ,
context : 'Authorization Error.'
} ] } ) ;
run ( ( ) => {
notifications . showAPIError ( error ) ;
} ) ;
let [ alert ] = notifications . alerts ;
expect ( alert . message ) . to . equal ( 'Authorization Error.' ) ;
expect ( alert . status ) . to . equal ( 'alert' ) ;
expect ( alert . type ) . to . equal ( 'error' ) ;
expect ( alert . key ) . to . equal ( 'api-error' ) ;
} ) ;
2022-05-27 13:52:39 +03:00
it ( '#showAPIError shows generic error for built-in error types' , function ( ) {
let notifications = this . owner . lookup ( 'service:notifications' ) ;
const error = new TypeError ( 'Testing' ) ;
notifications . showAPIError ( error ) ;
expect ( notifications . alerts [ 0 ] . message ) . to . equal ( GENERIC _ERROR _MESSAGE ) ;
} ) ;
2016-11-24 01:50:57 +03:00
it ( '#displayDelayed moves delayed notifications into content' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showNotification ( 'First' , { delayed : true } ) ;
notifications . showNotification ( 'Second' , { delayed : true } ) ;
notifications . showNotification ( 'Third' , { delayed : false } ) ;
notifications . displayDelayed ( ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . notifications ) . to . deep . equal ( [
2020-02-27 12:19:29 +03:00
{ message : 'Third' , status : 'notification' , type : undefined , key : undefined , actions : undefined , description : undefined , icon : undefined } ,
{ message : 'First' , status : 'notification' , type : undefined , key : undefined , actions : undefined , description : undefined , icon : undefined } ,
{ message : 'Second' , status : 'notification' , type : undefined , key : undefined , actions : undefined , description : undefined , icon : undefined }
2016-11-24 01:50:57 +03:00
] ) ;
} ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
it ( '#closeNotification removes POJO notifications' , function ( ) {
let notification = { message : 'Close test' , status : 'notification' } ;
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . handleNotification ( notification ) ;
} ) ;
2015-06-19 00:56:18 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . notifications )
2016-11-24 01:50:57 +03:00
. to . include ( notification ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . closeNotification ( notification ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . notifications )
2016-11-24 01:50:57 +03:00
. to . not . include ( notification ) ;
} ) ;
it ( '#closeNotification removes and deletes DS.Notification records' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2020-01-07 16:23:15 +03:00
let notification = NotificationStub . create ( { message : 'Close test' , status : 'alert' } ) ;
2016-11-24 01:50:57 +03:00
notification . deleteRecord = function ( ) { } ;
sinon . spy ( notification , 'deleteRecord' ) ;
notification . save = function ( ) {
return {
finally ( callback ) {
return callback ( notification ) ;
}
2015-06-19 00:56:18 +03:00
} ;
2016-11-24 01:50:57 +03:00
} ;
sinon . spy ( notification , 'save' ) ;
run ( ( ) => {
notifications . handleNotification ( notification ) ;
} ) ;
2015-06-19 00:56:18 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts ) . to . include ( notification ) ;
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . closeNotification ( notification ) ;
} ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
expect ( notification . deleteRecord . calledOnce ) . to . be . true ;
expect ( notification . save . calledOnce ) . to . be . true ;
2015-06-19 00:56:18 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts ) . to . not . include ( notification ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
it ( '#closeNotifications only removes notifications' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAlert ( 'First alert' ) ;
notifications . showNotification ( 'First notification' ) ;
2020-02-27 12:19:29 +03:00
notifications . showNotification ( 'Second notification' ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . length , 'alerts count' ) . to . equal ( 1 ) ;
expect ( notifications . notifications . length , 'notifications count' ) . to . equal ( 2 ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . closeNotifications ( ) ;
} ) ;
2015-06-19 00:56:18 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . length , 'alerts count' ) . to . equal ( 1 ) ;
expect ( notifications . notifications . length , 'notifications count' ) . to . equal ( 0 ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
it ( '#closeNotifications only closes notifications with specified key' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . showAlert ( 'First alert' ) ;
2020-02-27 12:19:29 +03:00
// using handleNotification as showNotification will auto-prune duplicates
2016-11-24 01:50:57 +03:00
notifications . handleNotification ( { message : 'First notification' , key : 'test.close' , status : 'notification' } ) ;
notifications . handleNotification ( { message : 'Second notification' , key : 'test.keep' , status : 'notification' } ) ;
notifications . handleNotification ( { message : 'Third notification' , key : 'test.close' , status : 'notification' } ) ;
2015-10-07 17:44:23 +03:00
} ) ;
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . closeNotifications ( 'test.close' ) ;
2015-06-19 00:56:18 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . notifications . length , 'notifications count' ) . to . equal ( 1 ) ;
expect ( notifications . notifications . firstObject . message , 'notification message' ) . to . equal ( 'Second notification' ) ;
expect ( notifications . alerts . length , 'alerts count' ) . to . equal ( 1 ) ;
2016-11-24 01:50:57 +03:00
} ) ;
it ( '#clearAll removes everything without deletion' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2016-11-24 01:50:57 +03:00
let notificationModel = EmberObject . create ( { message : 'model' } ) ;
notificationModel . deleteRecord = function ( ) { } ;
sinon . spy ( notificationModel , 'deleteRecord' ) ;
notificationModel . save = function ( ) {
return {
finally ( callback ) {
return callback ( notificationModel ) ;
}
2015-06-19 00:56:18 +03:00
} ;
2016-11-24 01:50:57 +03:00
} ;
sinon . spy ( notificationModel , 'save' ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
notifications . handleNotification ( notificationModel ) ;
notifications . handleNotification ( { message : 'pojo' } ) ;
2015-06-19 00:56:18 +03:00
2016-11-24 01:50:57 +03:00
notifications . clearAll ( ) ;
2015-06-19 00:56:18 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . content ) . to . be . empty ;
2016-11-24 01:50:57 +03:00
expect ( notificationModel . deleteRecord . called ) . to . be . false ;
expect ( notificationModel . save . called ) . to . be . false ;
} ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
it ( '#closeAlerts only removes alerts' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
notifications . showNotification ( 'First notification' ) ;
notifications . showAlert ( 'First alert' ) ;
notifications . showAlert ( 'Second alert' ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . closeAlerts ( ) ;
2015-10-07 17:44:23 +03:00
} ) ;
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . length ) . to . equal ( 0 ) ;
expect ( notifications . notifications . length ) . to . equal ( 1 ) ;
2016-11-24 01:50:57 +03:00
} ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
it ( '#closeAlerts closes only alerts with specified key' , function ( ) {
2019-05-13 15:43:53 +03:00
let notifications = this . owner . lookup ( 'service:notifications' ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
notifications . showNotification ( 'First notification' ) ;
notifications . showAlert ( 'First alert' , { key : 'test.close' } ) ;
notifications . showAlert ( 'Second alert' , { key : 'test.keep' } ) ;
notifications . showAlert ( 'Third alert' , { key : 'test.close' } ) ;
2015-10-07 17:44:23 +03:00
2016-11-24 01:50:57 +03:00
run ( ( ) => {
notifications . closeAlerts ( 'test.close' ) ;
2015-10-07 17:44:23 +03:00
} ) ;
2016-11-24 01:50:57 +03:00
2022-05-25 14:43:02 +03:00
expect ( notifications . alerts . length ) . to . equal ( 1 ) ;
expect ( notifications . alerts . firstObject . message ) . to . equal ( 'Second alert' ) ;
expect ( notifications . notifications . length ) . to . equal ( 1 ) ;
2016-11-24 01:50:57 +03:00
} ) ;
} ) ;