Skip to content

Commit 4bff66c

Browse files
carols10centsTurbo87
authored andcommitted
Frustrated trying to make a test with a logged in user
I have no idea what i'm doing
1 parent ccc3b80 commit 4bff66c

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

mirage/config.js

+7
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ export default function() {
206206
let user = schema.users.findBy({ login });
207207
return user ? user : notFound();
208208
});
209+
210+
this.get('/me', (schema) => {
211+
// magic number, create a user with this ID to have a current user logged in
212+
let user = schema.users.find(9000);
213+
214+
return user ? user : notFound();
215+
});
209216
}
210217

211218
function notFound() {

mirage/factories/user.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Factory, faker } from 'ember-cli-mirage';
2+
3+
export default Factory.extend({
4+
id(i) {
5+
return i;
6+
},
7+
email: () => faker.internet.email(),
8+
email_verified: () => true,
9+
email_verification_sent: () => true,
10+
11+
name(i) {
12+
return `Person ${i}`;
13+
},
14+
15+
login(i) {
16+
return `user_${i}`;
17+
},
18+
19+
avatar: () => faker.internet.avatar(),
20+
21+
url: () => faker.internet.url(),
22+
kind: () => 'user',
23+
});

mirage/models/user.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Model } from 'ember-cli-mirage';
2+
3+
export default Model;

tests/acceptance/dashboard-test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { test } from 'qunit';
2+
import moduleForAcceptance from 'cargo/tests/helpers/module-for-acceptance';
3+
import hasText from 'cargo/tests/helpers/has-text';
4+
5+
moduleForAcceptance('Acceptance | dashboard page', {
6+
needs: ['service:session']
7+
});
8+
9+
test('requires login', async function(assert) {
10+
server.loadFixtures();
11+
12+
await visit('/dashboard');
13+
14+
assert.equal(currentURL(), '/');
15+
hasText(assert, '#flash', 'Please log in to proceed');
16+
17+
});
18+
19+
test('has a dashboard page for the logged in user', async function(assert) {
20+
let user = server.create('user', { id: 9000 });
21+
22+
let session = this.application.__container__.lookup('service:session');
23+
session.loginUser(user);
24+
25+
await visit('/dashboard');
26+
27+
hasText(assert, '#flash', 'Please log in to proceed');
28+
29+
hasText(assert, '#crates-heading h1', 'My Dashboard');
30+
});

0 commit comments

Comments
 (0)