|
| 1 | +import { setupTest } from 'ember-qunit'; |
| 2 | +import { module, test } from 'qunit'; |
| 3 | + |
| 4 | +import setupMirage from '../helpers/setup-mirage'; |
| 5 | +import fetch from 'fetch'; |
| 6 | + |
| 7 | +module('Mirage | Categories', function(hooks) { |
| 8 | + setupTest(hooks); |
| 9 | + setupMirage(hooks); |
| 10 | + |
| 11 | + module('GET /api/v1/categories', function() { |
| 12 | + test('empty case', async function(assert) { |
| 13 | + let response = await fetch('/api/v1/categories'); |
| 14 | + assert.equal(response.status, 200); |
| 15 | + |
| 16 | + let responsePayload = await response.json(); |
| 17 | + assert.deepEqual(responsePayload, { |
| 18 | + categories: [], |
| 19 | + meta: { |
| 20 | + total: 0, |
| 21 | + }, |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + test('returns a paginated categories list', async function(assert) { |
| 26 | + this.server.create('category', { |
| 27 | + category: 'no-std', |
| 28 | + description: 'Crates that are able to function without the Rust standard library.', |
| 29 | + }); |
| 30 | + this.server.createList('category', 2); |
| 31 | + |
| 32 | + let response = await fetch('/api/v1/categories'); |
| 33 | + assert.equal(response.status, 200); |
| 34 | + |
| 35 | + let responsePayload = await response.json(); |
| 36 | + assert.deepEqual(responsePayload, { |
| 37 | + categories: [ |
| 38 | + { |
| 39 | + id: 'category-1', |
| 40 | + category: 'Category 1', |
| 41 | + crates_cnt: 0, |
| 42 | + created_at: '2010-06-16T21:30:45Z', |
| 43 | + description: 'This is the description for the category called "Category 1"', |
| 44 | + slug: 'category-1', |
| 45 | + }, |
| 46 | + { |
| 47 | + id: 'category-2', |
| 48 | + category: 'Category 2', |
| 49 | + crates_cnt: 0, |
| 50 | + created_at: '2010-06-16T21:30:45Z', |
| 51 | + description: 'This is the description for the category called "Category 2"', |
| 52 | + slug: 'category-2', |
| 53 | + }, |
| 54 | + { |
| 55 | + id: 'no-std', |
| 56 | + category: 'no-std', |
| 57 | + crates_cnt: 0, |
| 58 | + created_at: '2010-06-16T21:30:45Z', |
| 59 | + description: 'Crates that are able to function without the Rust standard library.', |
| 60 | + slug: 'no-std', |
| 61 | + }, |
| 62 | + ], |
| 63 | + meta: { |
| 64 | + total: 3, |
| 65 | + }, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + test('never returns more than 10 results', async function(assert) { |
| 70 | + this.server.createList('category', 25); |
| 71 | + |
| 72 | + let response = await fetch('/api/v1/categories'); |
| 73 | + assert.equal(response.status, 200); |
| 74 | + |
| 75 | + let responsePayload = await response.json(); |
| 76 | + assert.equal(responsePayload.categories.length, 10); |
| 77 | + assert.equal(responsePayload.meta.total, 25); |
| 78 | + }); |
| 79 | + |
| 80 | + test('supports `page` and `per_page` parameters', async function(assert) { |
| 81 | + this.server.createList('category', 25, { |
| 82 | + category: i => `cat-${String(i + 1).padStart(2, '0')}`, |
| 83 | + }); |
| 84 | + |
| 85 | + let response = await fetch('/api/v1/categories?page=2&per_page=5'); |
| 86 | + assert.equal(response.status, 200); |
| 87 | + |
| 88 | + let responsePayload = await response.json(); |
| 89 | + assert.equal(responsePayload.categories.length, 5); |
| 90 | + assert.deepEqual( |
| 91 | + responsePayload.categories.map(it => it.id), |
| 92 | + ['cat-06', 'cat-07', 'cat-08', 'cat-09', 'cat-10'], |
| 93 | + ); |
| 94 | + assert.equal(responsePayload.meta.total, 25); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + module('GET /api/v1/categories/:id', function() { |
| 99 | + test('returns 404 for unknown categories', async function(assert) { |
| 100 | + let response = await fetch('/api/v1/categories/foo'); |
| 101 | + assert.equal(response.status, 404); |
| 102 | + |
| 103 | + let responsePayload = await response.json(); |
| 104 | + assert.deepEqual(responsePayload, { errors: [{ detail: 'Not Found' }] }); |
| 105 | + }); |
| 106 | + |
| 107 | + test('returns a category object for known categories', async function(assert) { |
| 108 | + this.server.create('category', { |
| 109 | + category: 'no-std', |
| 110 | + description: 'Crates that are able to function without the Rust standard library.', |
| 111 | + }); |
| 112 | + |
| 113 | + let response = await fetch('/api/v1/categories/no-std'); |
| 114 | + assert.equal(response.status, 200); |
| 115 | + |
| 116 | + let responsePayload = await response.json(); |
| 117 | + assert.deepEqual(responsePayload, { |
| 118 | + category: { |
| 119 | + id: 'no-std', |
| 120 | + category: 'no-std', |
| 121 | + crates_cnt: 0, |
| 122 | + created_at: '2010-06-16T21:30:45Z', |
| 123 | + description: 'Crates that are able to function without the Rust standard library.', |
| 124 | + slug: 'no-std', |
| 125 | + }, |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + module('GET /api/v1/category_slugs', function() { |
| 131 | + test('empty case', async function(assert) { |
| 132 | + let response = await fetch('/api/v1/category_slugs'); |
| 133 | + assert.equal(response.status, 200); |
| 134 | + |
| 135 | + let responsePayload = await response.json(); |
| 136 | + assert.deepEqual(responsePayload, { |
| 137 | + category_slugs: [], |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + test('returns a category slugs list', async function(assert) { |
| 142 | + this.server.create('category', { |
| 143 | + category: 'no-std', |
| 144 | + description: 'Crates that are able to function without the Rust standard library.', |
| 145 | + }); |
| 146 | + this.server.createList('category', 2); |
| 147 | + |
| 148 | + let response = await fetch('/api/v1/category_slugs'); |
| 149 | + assert.equal(response.status, 200); |
| 150 | + |
| 151 | + let responsePayload = await response.json(); |
| 152 | + assert.deepEqual(responsePayload, { |
| 153 | + category_slugs: [ |
| 154 | + { |
| 155 | + description: 'This is the description for the category called "Category 1"', |
| 156 | + id: 'category-1', |
| 157 | + slug: 'category-1', |
| 158 | + }, |
| 159 | + { |
| 160 | + description: 'This is the description for the category called "Category 2"', |
| 161 | + id: 'category-2', |
| 162 | + slug: 'category-2', |
| 163 | + }, |
| 164 | + { |
| 165 | + description: 'Crates that are able to function without the Rust standard library.', |
| 166 | + id: 'no-std', |
| 167 | + slug: 'no-std', |
| 168 | + }, |
| 169 | + ], |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + test('has no pagination', async function(assert) { |
| 174 | + this.server.createList('category', 25); |
| 175 | + |
| 176 | + let response = await fetch('/api/v1/category_slugs'); |
| 177 | + assert.equal(response.status, 200); |
| 178 | + |
| 179 | + let responsePayload = await response.json(); |
| 180 | + assert.equal(responsePayload.category_slugs.length, 25); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments