|
| 1 | +import { module, test } from 'qunit'; |
| 2 | + |
| 3 | +import fetch from 'fetch'; |
| 4 | + |
| 5 | +import { setupTest } from '../../helpers'; |
| 6 | +import setupMirage from '../../helpers/setup-mirage'; |
| 7 | + |
| 8 | +module('Mirage | GET /api/v1/categories', function (hooks) { |
| 9 | + setupTest(hooks); |
| 10 | + setupMirage(hooks); |
| 11 | + |
| 12 | + test('empty case', async function (assert) { |
| 13 | + let response = await fetch('/api/v1/categories'); |
| 14 | + assert.equal(response.status, 200); |
| 15 | + assert.deepEqual(await response.json(), { |
| 16 | + categories: [], |
| 17 | + meta: { |
| 18 | + total: 0, |
| 19 | + }, |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + test('returns a paginated categories list', async function (assert) { |
| 24 | + this.server.create('category', { |
| 25 | + category: 'no-std', |
| 26 | + description: 'Crates that are able to function without the Rust standard library.', |
| 27 | + }); |
| 28 | + this.server.createList('category', 2); |
| 29 | + |
| 30 | + let response = await fetch('/api/v1/categories'); |
| 31 | + assert.equal(response.status, 200); |
| 32 | + assert.deepEqual(await response.json(), { |
| 33 | + categories: [ |
| 34 | + { |
| 35 | + id: 'category-1', |
| 36 | + category: 'Category 1', |
| 37 | + crates_cnt: 0, |
| 38 | + created_at: '2010-06-16T21:30:45Z', |
| 39 | + description: 'This is the description for the category called "Category 1"', |
| 40 | + slug: 'category-1', |
| 41 | + }, |
| 42 | + { |
| 43 | + id: 'category-2', |
| 44 | + category: 'Category 2', |
| 45 | + crates_cnt: 0, |
| 46 | + created_at: '2010-06-16T21:30:45Z', |
| 47 | + description: 'This is the description for the category called "Category 2"', |
| 48 | + slug: 'category-2', |
| 49 | + }, |
| 50 | + { |
| 51 | + id: 'no-std', |
| 52 | + category: 'no-std', |
| 53 | + crates_cnt: 0, |
| 54 | + created_at: '2010-06-16T21:30:45Z', |
| 55 | + description: 'Crates that are able to function without the Rust standard library.', |
| 56 | + slug: 'no-std', |
| 57 | + }, |
| 58 | + ], |
| 59 | + meta: { |
| 60 | + total: 3, |
| 61 | + }, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + test('never returns more than 10 results', async function (assert) { |
| 66 | + this.server.createList('category', 25); |
| 67 | + |
| 68 | + let response = await fetch('/api/v1/categories'); |
| 69 | + assert.equal(response.status, 200); |
| 70 | + |
| 71 | + let responsePayload = await response.json(); |
| 72 | + assert.equal(responsePayload.categories.length, 10); |
| 73 | + assert.equal(responsePayload.meta.total, 25); |
| 74 | + }); |
| 75 | + |
| 76 | + test('supports `page` and `per_page` parameters', async function (assert) { |
| 77 | + this.server.createList('category', 25, { |
| 78 | + category: i => `cat-${String(i + 1).padStart(2, '0')}`, |
| 79 | + }); |
| 80 | + |
| 81 | + let response = await fetch('/api/v1/categories?page=2&per_page=5'); |
| 82 | + assert.equal(response.status, 200); |
| 83 | + |
| 84 | + let responsePayload = await response.json(); |
| 85 | + assert.equal(responsePayload.categories.length, 5); |
| 86 | + assert.deepEqual( |
| 87 | + responsePayload.categories.map(it => it.id), |
| 88 | + ['cat-06', 'cat-07', 'cat-08', 'cat-09', 'cat-10'], |
| 89 | + ); |
| 90 | + assert.equal(responsePayload.meta.total, 25); |
| 91 | + }); |
| 92 | +}); |
0 commit comments