Skip to content

Commit 7c651bc

Browse files
committed
Add pagination specs
1 parent e90261b commit 7c651bc

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

app/controllers/group/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import Pagination from 'ember-cli-jsonapi-pagination/mixins/controllers/jsonapi-
33

44
export default Ember.Controller.extend(Pagination, {
55
totalPages: Ember.computed('size', 'number', 'model.posts.[]', function() {
6-
return parseInt(getParameterByName("page[number]", this.get("posts.links.last")));
6+
const pageParam =
7+
getParameterByName("page[number]", this.get("posts.links.last"));
8+
return parseInt(pageParam || 0);
79
})
810
});
911

@@ -12,8 +14,7 @@ function getParameterByName(name, url) {
1214
const regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
1315
const results = regex.exec(url);
1416

15-
if (!results) return null;
16-
if (!results[2]) return "";
17-
18-
return decodeURIComponent(results[2].replace(/\+/g, " "));
17+
if (results && results[2]) {
18+
return decodeURIComponent(results[2].replace(/\+/g, " "));
19+
}
1920
}

tests/acceptance/group/index-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ test("handles groups that do not exist", function(assert) {
2525

2626
test("renders appropriate group details", function(assert) {
2727
const group = server.create("group", { slug: "random" });
28-
const posts = server.createList("post", 3, { group });
28+
const posts = server.createList("post", 15, { group });
29+
server.createList("post", 5, { group });
2930

3031
page.visit({ slug: group.slug });
3132

@@ -36,6 +37,7 @@ test("renders appropriate group details", function(assert) {
3637
assert.equal(page.bannerUrl(), `https://res.cloudinary.com/${ENV['CLOUDINARY_CLOUD_NAME']}/image/fetch/w_1920/${group.bannerUrl}`);
3738
assert.equal(page.profilePictureUrl(), `https://res.cloudinary.com/${ENV['CLOUDINARY_CLOUD_NAME']}/image/fetch/w_200/${group.profilePictureUrl}`);
3839

40+
assert.equal(page.posts().count, 15);
3941
posts.forEach(function(post, i) {
4042
assert.equal(page.posts(i).title, posts[i].title);
4143
});

tests/unit/controllers/group/index-test.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@ moduleFor('controller:group/index', 'Unit | Controller | group/index', {
44
needs: ['service:metrics', 'service:router-scroll']
55
});
66

7-
// Replace this with your real tests.
8-
test('it exists', function(assert) {
9-
let controller = this.subject();
10-
assert.ok(controller);
7+
test('totalPages: Fetches the total pages from metadata', function(assert) {
8+
const model = {
9+
posts: {
10+
links: { last: "https://mcac.church/api/v1/posts?page%5Bnumber%5D=4" }
11+
}
12+
};
13+
14+
const controller = this.subject(model);
15+
16+
assert.equal(controller.get("totalPages"), 4);
17+
});
18+
19+
test('totalPages: Returns 0 when URL has no page number', function(assert) {
20+
const model = {
21+
posts: {
22+
links: { last: "https://mcac.church/api/v1/posts" }
23+
}
24+
};
25+
26+
const controller = this.subject(model);
27+
28+
assert.equal(controller.get("totalPages"), 0);
1129
});

0 commit comments

Comments
 (0)