From 7e1168cae222b04810f20f3d8491ab451d3c6cbb Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 13 Feb 2015 14:13:45 +0100 Subject: [PATCH] Add a simple acceptance test for dashboard and tooling needed to run it Apart from adding a test, this commit also adds a dummy implementation for Auth that can be used in tests to control the auth status. --- app/initializers/auth.coffee | 3 +- app/utils/computed-limit.js | 4 +- app/utils/test-auth.coffee | 51 ++++++++++++++++++++ bower.json | 3 +- package.json | 1 + tests/acceptance/dashboard-test.coffee | 67 ++++++++++++++++++++++++++ tests/helpers/start-app.js | 6 +++ 7 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 app/utils/test-auth.coffee create mode 100644 tests/acceptance/dashboard-test.coffee diff --git a/app/initializers/auth.coffee b/app/initializers/auth.coffee index 85e12452..befb2174 100644 --- a/app/initializers/auth.coffee +++ b/app/initializers/auth.coffee @@ -1,7 +1,8 @@ `import Auth from 'travis/utils/auth'` +`import TestAuth from 'travis/utils/test-auth'` initialize = (container, app) -> - app.register 'auth:main', Auth + app.register 'auth:main', if Ember.testing then TestAuth else Auth app.inject('route', 'auth', 'auth:main') app.inject('controller', 'auth', 'auth:main') diff --git a/app/utils/computed-limit.js b/app/utils/computed-limit.js index 3d769069..e594acef 100644 --- a/app/utils/computed-limit.js +++ b/app/utils/computed-limit.js @@ -2,7 +2,7 @@ import Ember from 'ember'; var limit = function(dependentKey, limitKey) { var options = { - addedItem: function(array, item, changeMeta, instanceMeta) { + addedItem: function(array, item, changeMeta) { var limit = Ember.get(this, limitKey); if (changeMeta.index < limit) { array.insertAt(changeMeta.index, item); @@ -12,7 +12,7 @@ var limit = function(dependentKey, limitKey) { } return array; }, - removedItem: function(array, item, changeMeta, instanceMeta) { + removedItem: function(array, item, changeMeta) { var limit = Ember.get(this, limitKey); if (changeMeta.index < limit && changeMeta.index < Ember.get(array, 'length')) { array.removeAt(changeMeta.index, 1); diff --git a/app/utils/test-auth.coffee b/app/utils/test-auth.coffee new file mode 100644 index 00000000..fdffd7a3 --- /dev/null +++ b/app/utils/test-auth.coffee @@ -0,0 +1,51 @@ +`import Ember from 'ember'` + +Auth = Ember.Object.extend + state: 'signed-out' + + # I want to disable auto sign in for tests for now, the plan is to either + # explicitly say that you're signed in or out (the latter being the default) + autoSignIn: (->) + + signOutForTests: -> + @set('state', 'signed-out') + @set('currentUser', null) + + signInForTests: (user) -> + @set('state', 'signed-in') + if user.constructor.typeKey? != 'user' + @store.pushPayload(users: [user]) + user = @store.recordForId('user', user.id) + + @set('currentUser', user) + + # TODO: we use these properties in templates, but there + # really should be something like a 'session' service that can be + # injected where we need it + userName: (-> + @get('currentUser.name') || @get('currentUser.login') + ).property('currentUser.login', 'currentUser.name') + + gravatarUrl: (-> + "#{location.protocol}//www.gravatar.com/avatar/#{@get('currentUser.gravatarId')}?s=48&d=mm" + ).property('currentUser.gravatarId') + + permissions: Ember.computed.alias('currentUser.permissions') + + signedIn: (-> + @get('state') == 'signed-in' + ).property('state') + + signedOut: (-> + @get('state') == 'signed-out' + ).property('state') + + signingIn: (-> + @get('state') == 'signing-in' + ).property('state') + + token: -> + if @get('state') == 'signed-in' + 'a-token' + +`export default Auth` diff --git a/bower.json b/bower.json index 9c9c3106..41e6caee 100644 --- a/bower.json +++ b/bower.json @@ -17,6 +17,7 @@ "JavaScript-MD5": "~1.1.0", "moment": "~2.9.0", "jquery-timeago": "~1.4.1", - "pusher": "~2.2.3" + "pusher": "~2.2.3", + "pretender": "0.1.0" } } diff --git a/package.json b/package.json index 9a53e777..2c46dc0c 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "ember-cli-ic-ajax": "0.1.1", "ember-cli-inject-live-reload": "^1.3.0", "ember-cli-inline-images": "^0.0.3", + "ember-cli-pretender": "0.3.1", "ember-cli-qunit": "0.3.0", "ember-cli-sauce": "0.0.7", "ember-cli-uglify": "1.0.1", diff --git a/tests/acceptance/dashboard-test.coffee b/tests/acceptance/dashboard-test.coffee new file mode 100644 index 00000000..41ede825 --- /dev/null +++ b/tests/acceptance/dashboard-test.coffee @@ -0,0 +1,67 @@ +`import Ember from 'ember'` +`import startApp from '../helpers/start-app'` +`import Pretender from 'pretender'` + +application = null +server = null + +module 'Acceptance: Dashboard', + setup: -> + application = startApp() + Ember.run -> + application.auth.signInForTests(id: 1, login: 'drogus') + server = new Pretender -> + @get('/v3/repos', (request) -> + data = { + "@type": "repositories", + "repositories": [{ + "@type": "repository", + "active": true, + "id": 1, + "name": "travis-web", + "slug": "travis-ci/travis-web", + "description": "The Ember web client for Travis CI", + "github_language": "CoffeeScript", + "private": false, + "owner": { + "@type": "organization", + "id": 1, + "login": "travis-ci" + }, + "last_build": { + "@type": "build", + "id": 1, + "number": "1", + "state": "passed", + "duration": 20, + "started_at": "2015-02-05T09:58:31Z", + "finished_at": "2015-02-05T10:09:10Z" + } + }, { + "@type": "repository", + "active": true, + "id": 2, + "name": "travis-test", + "slug": "travis-ci/travis-test", + "private": false, + "owner": { + "@type": "organization", + "id": 87, + "login": "travis-ci" + }, + "last_build": null + }] + } + return [200, { "Content-Type": "application/json" }, JSON.stringify(data)] + ) + + teardown: -> + Ember.run application, 'destroy' + server.shutdown() + +test 'visiting /dashboard', -> + visit '/dashboard' + + andThen -> + equal find('.tiles .repo').length, 1, 'there should be one repo displayed on dashboard' + equal find('.tiles .repo').text(), 'travis-web', 'travis-web repository should be displayed' diff --git a/tests/helpers/start-app.js b/tests/helpers/start-app.js index 16cc7c39..5e64fef6 100644 --- a/tests/helpers/start-app.js +++ b/tests/helpers/start-app.js @@ -15,5 +15,11 @@ export default function startApp(attrs) { application.injectTestHelpers(); }); + // TODO: I'm not sure if this is the best thing to do, but it seems + // easiest for now. That way in tests I can just write: + // + // application.auth.signInForTests() + application.auth = application.__container__.lookup('auth:main'); + return application; }