From c22a6dd48aadb360c40787cb4cd1e409d89e9de3 Mon Sep 17 00:00:00 2001 From: Nikola Begedin Date: Tue, 29 Nov 2016 17:29:37 +0100 Subject: [PATCH] Add redirect to projects.index from projects.donate, if donation already exists Rework flash messages do they don't clear on route transition Write test helpers for flash messages, to avoid stubbing the service --- app/components/category-item.js | 11 +--- app/components/member-list-item.js | 11 +--- app/components/organization-settings-form.js | 3 +- app/components/project-settings-form.js | 4 +- app/components/role-item.js | 11 +--- app/components/user-settings-form.js | 6 +-- app/controllers/project/donate.js | 10 ++-- app/routes/application.js | 6 --- app/routes/project/donate.js | 34 +++++++++++- app/routes/project/index.js | 21 +------- app/services/user-subscriptions.js | 32 +++++++++++ mirage/models/project.js | 1 - mirage/models/user.js | 2 +- tests/acceptance/project-donate-test.js | 26 +++++++++ tests/helpers/flash-message.js | 19 ++++++- .../components/category-item-test.js | 50 ++++++++--------- .../components/member-list-item-test.js | 54 +++++++++---------- .../organization-settings-form-test.js | 17 +++--- .../components/project-settings-form-test.js | 17 +++--- .../integration/components/role-item-test.js | 50 ++++++++--------- .../components/user-settings-form-test.js | 17 +++--- tests/unit/routes/application-test.js | 18 +------ 22 files changed, 218 insertions(+), 202 deletions(-) create mode 100644 app/services/user-subscriptions.js diff --git a/app/components/category-item.js b/app/components/category-item.js index e44d3cfbe..1b6a160c2 100644 --- a/app/components/category-item.js +++ b/app/components/category-item.js @@ -113,14 +113,7 @@ export default Component.extend({ * @private */ _flashError(message) { - let flashMessages = get(this, 'flashMessages'); - flashMessages.clearMessages(); - return flashMessages.add({ - message, - type: 'danger', - fixed: true, - sticky: false, - timeout: 5000 - }); + let options = { fixed: true, sticky: false, timeout: 5000 }; + get(this, 'flashMessages').clearMessages().danger(message, options); } }); diff --git a/app/components/member-list-item.js b/app/components/member-list-item.js index 72350aa43..15ad32e7e 100644 --- a/app/components/member-list-item.js +++ b/app/components/member-list-item.js @@ -30,14 +30,7 @@ export default Component.extend({ }, _flashSuccess(message) { - let flashMessages = get(this, 'flashMessages'); - flashMessages.clearMessages(); - return flashMessages.add({ - message, - type: 'success', - fixed: true, - sticky: false, - timeout: 5000 - }); + let options = { fixed: true, sticky: false, timeout: 5000 }; + get(this, 'flashMessages').clearMessages().success(message, options); } }); diff --git a/app/components/organization-settings-form.js b/app/components/organization-settings-form.js index d7ce0983b..13b46e59f 100644 --- a/app/components/organization-settings-form.js +++ b/app/components/organization-settings-form.js @@ -13,9 +13,8 @@ export default Component.extend({ actions: { save() { - let flashMessages = get(this, 'flashMessages'); get(this, 'organization').save().then(() => { - flashMessages.success('Organization updated successfully'); + get(this, 'flashMessages').clearMessages().success('Organization updated successfully'); }); } } diff --git a/app/components/project-settings-form.js b/app/components/project-settings-form.js index 9e6485bbf..d179c5a11 100644 --- a/app/components/project-settings-form.js +++ b/app/components/project-settings-form.js @@ -13,10 +13,8 @@ export default Component.extend({ actions: { save() { - let flashMessages = get(this, 'flashMessages'); - this.get('project').save().then(() => { - flashMessages.success('Project updated successfully'); + get(this, 'flashMessages').clearMessages().success('Project updated successfully'); }); } } diff --git a/app/components/role-item.js b/app/components/role-item.js index 51d331764..568adc3b7 100644 --- a/app/components/role-item.js +++ b/app/components/role-item.js @@ -49,14 +49,7 @@ export default Component.extend({ }, _flashError(message) { - let flashMessages = get(this, 'flashMessages'); - flashMessages.clearMessages(); - return flashMessages.add({ - message, - type: 'danger', - fixed: true, - sticky: false, - timeout: 5000 - }); + let options = { fixed: true, sticky: false, timeout: 5000 }; + get(this, 'flashMessages').clearMessages().danger(message, options); } }); diff --git a/app/components/user-settings-form.js b/app/components/user-settings-form.js index 1fd214883..46f2dd418 100644 --- a/app/components/user-settings-form.js +++ b/app/components/user-settings-form.js @@ -33,10 +33,8 @@ export default Component.extend({ @method save */ save() { - let flashMessages = get(this, 'flashMessages'); - - this.get('user').save().then(function() { - flashMessages.success('Profile updated successfully'); + this.get('user').save().then(() => { + get(this, 'flashMessages').clearMessages().success('Profile updated successfully'); }); } } diff --git a/app/controllers/project/donate.js b/app/controllers/project/donate.js index dd97726a1..a55c9edd0 100644 --- a/app/controllers/project/donate.js +++ b/app/controllers/project/donate.js @@ -14,6 +14,7 @@ const { const CUSTOMER_CREATION_ERROR = 'There was a problem in connecting your account with our payment processor. Please try again.'; const CARD_CREATION_ERROR = 'There was a problem in using your payment information. Please try again.'; const SUBSCRIPTION_CREATION_ERROR = 'There was a problem in setting up your monthly donation. Please try again.'; +const SUBSCRIPTION_VALIDATION_ERROR = "The amount you've set for your monthly donation is invalid."; export default Controller.extend({ amount: null, @@ -132,13 +133,8 @@ export default Controller.extend({ }, _handleSubscriptionCreationError(response) { - let friendlyError; - - if (isValidationError(response)) { - friendlyError = new FriendlyError('The amount you\'ve set for your monthly donation is invalid.'); - } else { - friendlyError = new FriendlyError(SUBSCRIPTION_CREATION_ERROR); - } + let message = isValidationError(response) ? SUBSCRIPTION_VALIDATION_ERROR : SUBSCRIPTION_CREATION_ERROR; + let friendlyError = new FriendlyError(message); return RSVP.reject(friendlyError); }, diff --git a/app/routes/application.js b/app/routes/application.js index 8b1bd260a..950ad2fd1 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -170,12 +170,6 @@ export default Route.extend(ApplicationRouteMixin, { }, actions: { - didTransition() { - // Clear flash messages on every transition - this.get('flashMessages').clearMessages(); - return true; // Bubble the event - }, - willTransition(transition) { if (this._shouldTransitionToOnboardingRoute(transition)) { this._abortAndFixHistory(transition); diff --git a/app/routes/project/donate.js b/app/routes/project/donate.js index 16240adb3..65c3ca6fd 100644 --- a/app/routes/project/donate.js +++ b/app/routes/project/donate.js @@ -2,7 +2,37 @@ import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-rout import Ember from 'ember'; const { - Route + get, + inject: { service }, + Route, + RSVP } = Ember; -export default Route.extend(AuthenticatedRouteMixin, {}); +const ALREADY_A_SUBSCRIBER = "You're already supporting this project."; + +export default Route.extend(AuthenticatedRouteMixin, { + flashMessages: service(), + userSubscriptions: service(), + + model() { + let project = this.modelFor('project'); + + return RSVP.hash({ + project, + subscription: this.get('userSubscriptions').fetchForProject(project) + }); + }, + + afterModel({ project, subscription }) { + if (subscription) { + get(this, 'flashMessages').success(ALREADY_A_SUBSCRIBER); + this.transitionTo('project', project); + } else { + this._super.call(...arguments); + } + }, + + setupController(controller, models) { + controller.setProperties(models); + } +}); diff --git a/app/routes/project/index.js b/app/routes/project/index.js index 74d18abbe..fe88e4296 100644 --- a/app/routes/project/index.js +++ b/app/routes/project/index.js @@ -7,35 +7,18 @@ const { } = Ember; export default Route.extend({ - currentUser: service(), + userSubscriptions: service(), model() { let project = this.modelFor('project'); return RSVP.hash({ project, - subscription: this._fetchCurrentUserSubscriptionFor(project) + subscription: this.get('userSubscriptions').fetchForProject(project) }); }, setupController(controller, models) { controller.setProperties(models); - }, - - _fetchCurrentUserSubscriptionFor(project) { - let user = this.get('currentUser.user'); - - if (user) { - let subscriptions = user.get('stripeConnectSubscriptions'); - let planId = project.belongsTo('stripeConnectPlan').id(); - return RSVP.hash({ subscriptions, planId }).then(({ subscriptions, planId }) => { - let subscription = subscriptions.find((subscription) => { - return subscription.belongsTo('stripeConnectPlan').id() === planId; - }); - return RSVP.resolve(subscription); - }); - } else { - return null; - } } }); diff --git a/app/services/user-subscriptions.js b/app/services/user-subscriptions.js new file mode 100644 index 000000000..eb43f38d0 --- /dev/null +++ b/app/services/user-subscriptions.js @@ -0,0 +1,32 @@ +import Ember from 'ember'; + +const { + computed, + inject: { service }, + RSVP, + Service +} = Ember; + +export default Service.extend({ + currentUser: service(), + store: service(), + + user: computed.alias('currentUser.user'), + + fetchForProject(project) { + let user = this.get('user'); + + if (user) { + let subscriptions = user.get('stripeConnectSubscriptions'); + let planId = project.belongsTo('stripeConnectPlan').id(); + return RSVP.hash({ subscriptions, planId }).then(({ subscriptions, planId }) => { + let subscription = subscriptions.find((subscription) => { + return subscription.belongsTo('stripeConnectPlan').id() === planId; + }); + return RSVP.resolve(subscription); + }); + } else { + return null; + } + } +}); diff --git a/mirage/models/project.js b/mirage/models/project.js index e136903ea..153f3e426 100644 --- a/mirage/models/project.js +++ b/mirage/models/project.js @@ -1,7 +1,6 @@ import { Model, belongsTo, hasMany } from 'ember-cli-mirage'; export default Model.extend({ - currentDonationGoal: belongsTo('donation-goal'), donationGoals: hasMany(), organization: belongsTo(), tasks: hasMany(), diff --git a/mirage/models/user.js b/mirage/models/user.js index 3db68f6ef..8bce13cc4 100644 --- a/mirage/models/user.js +++ b/mirage/models/user.js @@ -4,7 +4,7 @@ export default Model.extend({ organizationMemberships: hasMany({ inverse: 'member' }), stripePlatformCard: belongsTo('stripe-platform-card'), stripePlatformCustomer: belongsTo('stripe-platform-customer'), - subscriptions: hasMany('stripe-connect-subscription'), + stripeConnectSubscriptions: hasMany('stripe-connect-subscription'), userCategories: hasMany(), userRoles: hasMany(), userSkills: hasMany() diff --git a/tests/acceptance/project-donate-test.js b/tests/acceptance/project-donate-test.js index 74cd2c9ab..d42aea8d0 100644 --- a/tests/acceptance/project-donate-test.js +++ b/tests/acceptance/project-donate-test.js @@ -4,6 +4,7 @@ import Ember from 'ember'; import Mirage from 'ember-cli-mirage'; import { authenticateSession } from 'code-corps-ember/tests/helpers/ember-simple-auth'; +import { getFlashMessageCount } from 'code-corps-ember/tests/helpers/flash-message'; import createOrganizationWithSluggedRoute from 'code-corps-ember/tests/helpers/mirage/create-organization-with-slugged-route'; import projectDonatePage from '../pages/project/donate'; @@ -85,6 +86,31 @@ test('It requires authentication', function(assert) { }); }); +test('It redirects to project route if already a subscriber, with a flash', function(assert) { + assert.expect(2); + + let user = server.create('user'); + authenticateSession(this.application, { 'user_id': user.id }); + + let organization = createOrganizationWithSluggedRoute(); + let project = server.create('project', { organization }); + + let stripeConnectPlan = project.createStripeConnectPlan({ project }); + + server.create('stripeConnectSubscription', { stripeConnectPlan, user }); + + projectDonatePage.visit({ + amount: 10, + organization: organization.slug, + project: project.slug + }); + + andThen(() => { + assert.equal(getFlashMessageCount(this), 1, 'A flash message was shown.'); + assert.equal(currentRouteName(), 'project.index', 'User was redirected to index'); + }); +}); + test('Allows creating a card and donating (creating a subscription)', function(assert) { assert.expect(8); diff --git a/tests/helpers/flash-message.js b/tests/helpers/flash-message.js index 419b4cae1..712be9450 100644 --- a/tests/helpers/flash-message.js +++ b/tests/helpers/flash-message.js @@ -1,6 +1,23 @@ import Ember from 'ember'; import FlashObject from 'ember-cli-flash/flash/object'; -const { K } = Ember; +const { getOwner, K } = Ember; FlashObject.reopen({ init: K }); + +export function getFlashMessageCount(context) { + return getTestContainer(context).lookup('service:flash-messages').get('queue').length; +} + +export function getFlashMessageAt(index, context) { + return getTestContainer(context).lookup('service:flash-messages').get('queue')[index]; +} + +function getTestContainer(context) { + if (context.application) { // acceptance test + return context.application.__container__; + + } else { // integration/unit test + return getOwner(context); + } +} diff --git a/tests/integration/components/category-item-test.js b/tests/integration/components/category-item-test.js index 7b7c9f4ed..2f88a9f96 100644 --- a/tests/integration/components/category-item-test.js +++ b/tests/integration/components/category-item-test.js @@ -3,6 +3,7 @@ import hbs from 'htmlbars-inline-precompile'; import Ember from 'ember'; import wait from 'ember-test-helpers/wait'; import stubService from 'code-corps-ember/tests/helpers/stub-service'; +import { getFlashMessageCount, getFlashMessageAt } from 'code-corps-ember/tests/helpers/flash-message'; const { getOwner, @@ -15,6 +16,7 @@ moduleForComponent('category-item', 'Integration | Component | category item', { integration: true, beforeEach() { mockUserCategory.set('categoryId', defaultCategoryId); + getOwner(this).lookup('service:flash-messages').registerTypes(['danger']); } }); @@ -124,58 +126,50 @@ test('it works for removing selected categories', function(assert) { test('it creates a flash message on an error when adding', function(assert) { let done = assert.async(); - assert.expect(7); + assert.expect(4); stubService(this, 'user-categories', mockUserCategoriesServiceForErrors); this.set('category', unselectedCategory); - stubService(this, 'flash-messages', { - clearMessages() { - assert.ok(true); - }, - add(object) { - assert.ok(object.message.indexOf(unselectedCategory.name) !== -1); - assert.equal(object.type, 'danger'); - assert.equal(object.fixed, true); - assert.equal(object.sticky, false); - assert.equal(object.timeout, 5000); - } - }); - this.render(hbs`{{category-item category=category}}`); this.$('button').click(); wait().then(() => { assert.notOk(this.$('span').hasClass('button-spinner')); + + assert.equal(getFlashMessageCount(this), 1, 'One message is shown'); + + let flash = getFlashMessageAt(0, this); + let actualOptions = flash.getProperties('fixed', 'sticky', 'timeout', 'type'); + let expectedOptions = { fixed: true, sticky: false, timeout: 5000, type: 'danger' }; + assert.deepEqual(actualOptions, expectedOptions, 'Proper message was set'); + assert.ok(flash.message.indexOf(unselectedCategory.name) !== -1, 'Message text includes the category name'); + done(); }); }); test('it creates a flash message on an error when removing', function(assert) { let done = assert.async(); - assert.expect(7); + assert.expect(4); stubService(this, 'user-categories', mockUserCategoriesServiceForErrors); this.set('category', selectedCategory); - stubService(this, 'flash-messages', { - clearMessages() { - assert.ok(true); - }, - add(object) { - assert.ok(object.message.indexOf(selectedCategory.name) !== -1); - assert.equal(object.type, 'danger'); - assert.equal(object.fixed, true); - assert.equal(object.sticky, false); - assert.equal(object.timeout, 5000); - } - }); - this.render(hbs`{{category-item category=category}}`); this.$('button').click(); wait().then(() => { assert.notOk(this.$('span').hasClass('button-spinner')); + + assert.equal(getFlashMessageCount(this), 1, 'One message is shown'); + + let flash = getFlashMessageAt(0, this); + let actualOptions = flash.getProperties('fixed', 'sticky', 'timeout', 'type'); + let expectedOptions = { fixed: true, sticky: false, timeout: 5000, type: 'danger' }; + assert.deepEqual(actualOptions, expectedOptions, 'Proper message was set'); + assert.ok(flash.message.indexOf(selectedCategory.name) !== -1, 'Message text includes the category name'); + done(); }); }); diff --git a/tests/integration/components/member-list-item-test.js b/tests/integration/components/member-list-item-test.js index 56714a853..c7ea0622d 100644 --- a/tests/integration/components/member-list-item-test.js +++ b/tests/integration/components/member-list-item-test.js @@ -1,9 +1,10 @@ import { moduleForComponent, test } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; import Ember from 'ember'; -import stubService from 'code-corps-ember/tests/helpers/stub-service'; +import { getFlashMessageCount, getFlashMessageAt } from 'code-corps-ember/tests/helpers/flash-message'; const { + getOwner, Object, RSVP } = Ember; @@ -52,7 +53,10 @@ function mockMembership(pending) { } moduleForComponent('member-list-item', 'Integration | Component | member list item', { - integration: true + integration: true, + beforeEach() { + getOwner(this).lookup('service:flash-messages').registerTypes(['success']); + } }); test('it renders the basic information for the user', function(assert) { @@ -105,7 +109,7 @@ test('it does not render the buttons when not pending', function(assert) { }); test('it sends the approve action when clicking approve', function(assert) { - assert.expect(7); + assert.expect(4); let membership = Object.create({ isPending: true, @@ -118,26 +122,21 @@ test('it sends the approve action when clicking approve', function(assert) { this.set('membership', membership); this.set('user', user); - stubService(this, 'flash-messages', { - clearMessages() { - assert.ok(true); - }, - add(object) { - assert.ok(object.message.indexOf('Membership approved') !== -1); - assert.equal(object.type, 'success'); - assert.equal(object.fixed, true); - assert.equal(object.sticky, false); - assert.equal(object.timeout, 5000); - } - }); - this.render(hbs`{{member-list-item membership=membership user=user}}`); this.$('button.default').click(); + + assert.equal(getFlashMessageCount(this), 1, 'One flash message is rendered'); + + let flash = getFlashMessageAt(0, this); + let actualOptions = flash.getProperties('fixed', 'sticky', 'timeout', 'type'); + let expectedOptions = { fixed: true, sticky: false, timeout: 5000, type: 'success' }; + assert.deepEqual(actualOptions, expectedOptions, 'Proper message was set'); + assert.ok(flash.message.indexOf('Membership approved') !== -1, 'Message includes proper text'); }); test('it sends the deny action when clicking deny', function(assert) { - assert.expect(7); + assert.expect(4); window.confirm = function() { return true; @@ -151,23 +150,18 @@ test('it sends the deny action when clicking deny', function(assert) { } }); - stubService(this, 'flash-messages', { - clearMessages() { - assert.ok(true); - }, - add(object) { - assert.ok(object.message.indexOf('Membership denied') !== -1); - assert.equal(object.type, 'success'); - assert.equal(object.fixed, true); - assert.equal(object.sticky, false); - assert.equal(object.timeout, 5000); - } - }); - this.set('membership', membership); this.set('user', user); this.render(hbs`{{member-list-item membership=membership user=user}}`); this.$('button.danger').click(); + + assert.equal(getFlashMessageCount(this), 1, 'One flash message is rendered'); + + let flash = getFlashMessageAt(0, this); + let actualOptions = flash.getProperties('fixed', 'sticky', 'timeout', 'type'); + let expectedOptions = { fixed: true, sticky: false, timeout: 5000, type: 'success' }; + assert.deepEqual(actualOptions, expectedOptions, 'Proper message was set'); + assert.ok(flash.message.indexOf('Membership denied') !== -1, 'Message includes proper text'); }); diff --git a/tests/integration/components/organization-settings-form-test.js b/tests/integration/components/organization-settings-form-test.js index 757056e4e..1b8ece4ec 100644 --- a/tests/integration/components/organization-settings-form-test.js +++ b/tests/integration/components/organization-settings-form-test.js @@ -1,12 +1,15 @@ import Ember from 'ember'; import { moduleForComponent, test } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; -import stubService from 'code-corps-ember/tests/helpers/stub-service'; +import { getFlashMessageCount } from 'code-corps-ember/tests/helpers/flash-message'; -const { RSVP } = Ember; +const { getOwner, RSVP } = Ember; moduleForComponent('organization-settings-form', 'Integration | Component | organization settings form', { - integration: true + integration: true, + beforeEach() { + getOwner(this).lookup('service:flash-messages').registerTypes(['success']); + } }); let organization = { @@ -45,13 +48,9 @@ test('it calls save on organization when save button is clicked', function(asser this.set('organization', organization); - stubService(this, 'flash-messages', { - success() { - assert.ok(true, 'Flash message service was called'); - } - }); - this.render(hbs`{{organization-settings-form organization=organization}}`); this.$('.save').click(); + + assert.equal(getFlashMessageCount(this), 1, 'A flash message was shown'); }); diff --git a/tests/integration/components/project-settings-form-test.js b/tests/integration/components/project-settings-form-test.js index 823cfda95..90f5ee40f 100644 --- a/tests/integration/components/project-settings-form-test.js +++ b/tests/integration/components/project-settings-form-test.js @@ -1,12 +1,15 @@ import Ember from 'ember'; import { moduleForComponent, test } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; -import stubService from 'code-corps-ember/tests/helpers/stub-service'; +import { getFlashMessageCount } from 'code-corps-ember/tests/helpers/flash-message'; -const { RSVP } = Ember; +const { getOwner, RSVP } = Ember; moduleForComponent('project-settings-form', 'Integration | Component | project settings form', { - integration: true + integration: true, + beforeEach() { + getOwner(this).lookup('service:flash-messages').registerTypes(['success']); + } }); let project = { @@ -45,13 +48,9 @@ test('it calls save on project when save button is clicked', function(assert) { this.set('project', project); - stubService(this, 'flash-messages', { - success() { - assert.ok(true, 'Flash message service was called'); - } - }); - this.render(hbs`{{project-settings-form project=project}}`); this.$('.save').click(); + + assert.equal(getFlashMessageCount(this), 1, 'A flash message was shown'); }); diff --git a/tests/integration/components/role-item-test.js b/tests/integration/components/role-item-test.js index b8ba77a58..ea192ff90 100644 --- a/tests/integration/components/role-item-test.js +++ b/tests/integration/components/role-item-test.js @@ -3,6 +3,7 @@ import hbs from 'htmlbars-inline-precompile'; import Ember from 'ember'; import wait from 'ember-test-helpers/wait'; import stubService from 'code-corps-ember/tests/helpers/stub-service'; +import { getFlashMessageCount, getFlashMessageAt } from 'code-corps-ember/tests/helpers/flash-message'; const { getOwner, @@ -77,6 +78,7 @@ moduleForComponent('role-item', 'Integration | Component | role item', { integration: true, beforeEach() { mockUserRole.set('roleId', defaultRoleId); + getOwner(this).lookup('service:flash-messages').registerTypes(['danger']); } }); @@ -120,58 +122,50 @@ test('it works for removing selected roles', function(assert) { test('it creates a flash message on an error when adding', function(assert) { let done = assert.async(); - assert.expect(7); + assert.expect(4); stubService(this, 'user-roles', mockUserRolesServiceForErrors); this.set('role', unselectedRole); - stubService(this, 'flash-messages', { - clearMessages() { - assert.ok(true); - }, - add(object) { - assert.ok(object.message.indexOf(unselectedRole.name) !== -1); - assert.equal(object.type, 'danger'); - assert.equal(object.fixed, true); - assert.equal(object.sticky, false); - assert.equal(object.timeout, 5000); - } - }); - this.render(hbs`{{role-item role=role}}`); this.$('button').click(); wait().then(() => { assert.notOk(this.$('span').hasClass('button-spinner')); + + assert.equal(getFlashMessageCount(this), 1, 'One flash message is rendered'); + + let flash = getFlashMessageAt(0, this); + let actualOptions = flash.getProperties('fixed', 'sticky', 'timeout', 'type'); + let expectedOptions = { fixed: true, sticky: false, timeout: 5000, type: 'danger' }; + assert.deepEqual(actualOptions, expectedOptions, 'Proper message was set'); + assert.ok(flash.message.indexOf(unselectedRole.name) !== -1, 'Message text includes the role name'); + done(); }); }); test('it creates a flash message on an error when removing', function(assert) { let done = assert.async(); - assert.expect(7); + assert.expect(4); stubService(this, 'user-roles', mockUserRolesServiceForErrors); this.set('role', selectedRole); - stubService(this, 'flash-messages', { - clearMessages() { - assert.ok(true); - }, - add(object) { - assert.ok(object.message.indexOf(selectedRole.name) !== -1); - assert.equal(object.type, 'danger'); - assert.equal(object.fixed, true); - assert.equal(object.sticky, false); - assert.equal(object.timeout, 5000); - } - }); - this.render(hbs`{{role-item role=role}}`); this.$('button').click(); wait().then(() => { assert.notOk(this.$('span').hasClass('button-spinner')); + + assert.equal(getFlashMessageCount(this), 1, 'One flash message is rendered'); + + let flash = getFlashMessageAt(0, this); + let actualOptions = flash.getProperties('fixed', 'sticky', 'timeout', 'type'); + let expectedOptions = { fixed: true, sticky: false, timeout: 5000, type: 'danger' }; + assert.deepEqual(actualOptions, expectedOptions, 'Proper message was set'); + assert.ok(flash.message.indexOf(selectedRole.name) !== -1, 'Message text includes the role name'); + done(); }); }); diff --git a/tests/integration/components/user-settings-form-test.js b/tests/integration/components/user-settings-form-test.js index 07497ad43..2d17d659b 100644 --- a/tests/integration/components/user-settings-form-test.js +++ b/tests/integration/components/user-settings-form-test.js @@ -1,12 +1,15 @@ import Ember from 'ember'; import { moduleForComponent, test } from 'ember-qunit'; import hbs from 'htmlbars-inline-precompile'; -import stubService from 'code-corps-ember/tests/helpers/stub-service'; +import { getFlashMessageCount } from 'code-corps-ember/tests/helpers/flash-message'; -const { RSVP } = Ember; +const { getOwner, RSVP } = Ember; moduleForComponent('user-settings-form', 'Integration | Component | user settings form', { - integration: true + integration: true, + beforeEach() { + getOwner(this).lookup('service:flash-messages').registerTypes(['success']); + } }); test('it renders', function(assert) { @@ -51,13 +54,9 @@ test('it calls save on user when save button is clicked', function(assert) { this.set('user', user); - stubService(this, 'flash-messages', { - success() { - assert.ok(true, 'Flash message service was called'); - } - }); - this.render(hbs`{{user-settings-form user=user}}`); this.$('.save').click(); + + assert.equal(getFlashMessageCount(this), 1, 'A flash message was shown'); }); diff --git a/tests/unit/routes/application-test.js b/tests/unit/routes/application-test.js index 05b487577..e8beb1012 100644 --- a/tests/unit/routes/application-test.js +++ b/tests/unit/routes/application-test.js @@ -1,7 +1,4 @@ import { moduleFor, test } from 'ember-qunit'; -import Ember from 'ember'; - -const { getOwner } = Ember; moduleFor('route:application', 'Unit | Route | application', { // Specify the other units that are required for this test. @@ -12,18 +9,7 @@ moduleFor('route:application', 'Unit | Route | application', { ] }); -test('it clears flash messages on transition', function(assert) { - assert.expect(2); - - let typesUsed = ['success']; - let flashMessages = getOwner(this).lookup('service:flash-messages'); - flashMessages.registerTypes(typesUsed); - +test('it exists', function(assert) { let route = this.subject(); - - flashMessages.success('Success!'); - assert.equal(flashMessages.get('queue.length'), 1); - - route.send('didTransition'); - assert.equal(flashMessages.get('queue.length'), 0); + assert.ok(route); });