Skip to content

Reorganize Mirage test cases #3847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 0 additions & 207 deletions tests/mirage/categories-test.js

This file was deleted.

57 changes: 57 additions & 0 deletions tests/mirage/categories/get-by-id-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { module, test } from 'qunit';

import fetch from 'fetch';

import { setupTest } from '../../helpers';
import setupMirage from '../../helpers/setup-mirage';

module('Mirage | GET /api/v1/categories/:id', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

test('returns 404 for unknown categories', async function (assert) {
let response = await fetch('/api/v1/categories/foo');
assert.equal(response.status, 404);
assert.deepEqual(await response.json(), { errors: [{ detail: 'Not Found' }] });
});

test('returns a category object for known categories', async function (assert) {
this.server.create('category', {
category: 'no-std',
description: 'Crates that are able to function without the Rust standard library.',
});

let response = await fetch('/api/v1/categories/no-std');
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), {
category: {
id: 'no-std',
category: 'no-std',
crates_cnt: 0,
created_at: '2010-06-16T21:30:45Z',
description: 'Crates that are able to function without the Rust standard library.',
slug: 'no-std',
},
});
});

test('calculates `crates_cnt` correctly', async function (assert) {
this.server.create('category', { category: 'cli' });
this.server.createList('crate', 7, { categoryIds: ['cli'] });
this.server.create('category', { category: 'not-cli' });
this.server.createList('crate', 3, { categoryIds: ['not-cli'] });

let response = await fetch('/api/v1/categories/cli');
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), {
category: {
category: 'cli',
crates_cnt: 7,
created_at: '2010-06-16T21:30:45Z',
description: 'This is the description for the category called "cli"',
id: 'cli',
slug: 'cli',
},
});
});
});
92 changes: 92 additions & 0 deletions tests/mirage/categories/list-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { module, test } from 'qunit';

import fetch from 'fetch';

import { setupTest } from '../../helpers';
import setupMirage from '../../helpers/setup-mirage';

module('Mirage | GET /api/v1/categories', function (hooks) {
setupTest(hooks);
setupMirage(hooks);

test('empty case', async function (assert) {
let response = await fetch('/api/v1/categories');
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), {
categories: [],
meta: {
total: 0,
},
});
});

test('returns a paginated categories list', async function (assert) {
this.server.create('category', {
category: 'no-std',
description: 'Crates that are able to function without the Rust standard library.',
});
this.server.createList('category', 2);

let response = await fetch('/api/v1/categories');
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), {
categories: [
{
id: 'category-1',
category: 'Category 1',
crates_cnt: 0,
created_at: '2010-06-16T21:30:45Z',
description: 'This is the description for the category called "Category 1"',
slug: 'category-1',
},
{
id: 'category-2',
category: 'Category 2',
crates_cnt: 0,
created_at: '2010-06-16T21:30:45Z',
description: 'This is the description for the category called "Category 2"',
slug: 'category-2',
},
{
id: 'no-std',
category: 'no-std',
crates_cnt: 0,
created_at: '2010-06-16T21:30:45Z',
description: 'Crates that are able to function without the Rust standard library.',
slug: 'no-std',
},
],
meta: {
total: 3,
},
});
});

test('never returns more than 10 results', async function (assert) {
this.server.createList('category', 25);

let response = await fetch('/api/v1/categories');
assert.equal(response.status, 200);

let responsePayload = await response.json();
assert.equal(responsePayload.categories.length, 10);
assert.equal(responsePayload.meta.total, 25);
});

test('supports `page` and `per_page` parameters', async function (assert) {
this.server.createList('category', 25, {
category: i => `cat-${String(i + 1).padStart(2, '0')}`,
});

let response = await fetch('/api/v1/categories?page=2&per_page=5');
assert.equal(response.status, 200);

let responsePayload = await response.json();
assert.equal(responsePayload.categories.length, 5);
assert.deepEqual(
responsePayload.categories.map(it => it.id),
['cat-06', 'cat-07', 'cat-08', 'cat-09', 'cat-10'],
);
assert.equal(responsePayload.meta.total, 25);
});
});
Loading