* Basics might be working? * Stop propagating button clicks in autocomplete pills * Better blank slate * Better location to stop propagation * Remove author_id from org co-authors * Move UserStore and try testing it * Remove extraneous comments * OH... that's what that does! * Very basic testing * Re-organize javascripts * Rename & re-org for testing * Cleanup * More tests * Remove unnecessary nesting * Coninuing to try to bump coverage * Include /packs/ in code coverage metric * Try tweaking jest coverage more? We probably can't collect coverage from all of packs/* (because coverage is too low) but maybe we can try to opt-in for newer areas as we go? * Relocate JS tests, for build & coverage * User ID exception on search, not fetch * Remove commented-out console.log --------- Co-authored-by: Mac Siri <krairit.siri@gmail.com>
79 lines
2.5 KiB
JavaScript
79 lines
2.5 KiB
JavaScript
import fetch from 'jest-fetch-mock';
|
|
import { UserStore } from '../UserStore';
|
|
|
|
global.fetch = fetch;
|
|
|
|
function fakeUsers() {
|
|
return JSON.stringify([
|
|
{ name: 'Alice', username: 'alice', id: 1 },
|
|
{ name: 'Bob', username: 'bob', id: 2 },
|
|
{ name: 'Charlie', username: 'charlie', id: 3 },
|
|
{ name: 'Almost Alice', username: 'almostalice', id: 4 },
|
|
]);
|
|
}
|
|
|
|
describe('UserStore', () => {
|
|
beforeEach(() => {
|
|
fetch.resetMocks();
|
|
fetch.mockResponse(fakeUsers());
|
|
});
|
|
|
|
test('initializes with an empty user list', () => {
|
|
const subject = new UserStore();
|
|
expect(subject.users).toStrictEqual([]);
|
|
});
|
|
|
|
test('initializes unfetched', () => {
|
|
const subject = new UserStore();
|
|
expect(subject.wasFetched).toBeFalsy();
|
|
});
|
|
|
|
test('fetch from a given url', () => {
|
|
new UserStore().fetch('/path/to/the/users');
|
|
expect(fetch).toHaveBeenCalledWith('/path/to/the/users');
|
|
});
|
|
|
|
test('only fetches once', async () => {
|
|
const subject = new UserStore();
|
|
await subject.fetch('/path/to/the/users');
|
|
expect(subject.wasFetched).toBeTruthy();
|
|
await subject.fetch('/path/to/the/users');
|
|
await subject.fetch('/path/to/the/users');
|
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test('return a sub-set of users matching given IDs', async () => {
|
|
const subject = new UserStore();
|
|
await subject.fetch('/path/to/the/users');
|
|
expect(subject.matchingIds(['1', '4'])).toStrictEqual([
|
|
{ name: 'Alice', username: 'alice', id: 1 },
|
|
{ name: 'Almost Alice', username: 'almostalice', id: 4 },
|
|
]);
|
|
expect(subject.matchingIds(['2', '3'])).toStrictEqual([
|
|
{ name: 'Bob', username: 'bob', id: 2 },
|
|
{ name: 'Charlie', username: 'charlie', id: 3 },
|
|
]);
|
|
});
|
|
|
|
test('return a sub-set of users matching search term', async () => {
|
|
const subject = new UserStore();
|
|
await subject.fetch('/path/to/the/users');
|
|
expect(subject.search('alice')).toStrictEqual([
|
|
{ name: 'Alice', username: 'alice', id: 1 },
|
|
{ name: 'Almost Alice', username: 'almostalice', id: 4 },
|
|
]);
|
|
expect(subject.search('stal')).toStrictEqual([
|
|
{ name: 'Almost Alice', username: 'almostalice', id: 4 },
|
|
]);
|
|
expect(subject.search('david')).toStrictEqual([]);
|
|
});
|
|
|
|
test('search with an exception', async () => {
|
|
const subject = new UserStore();
|
|
await subject.fetch('/path/to/the/users');
|
|
expect(subject.search('a', { except: '3' })).toStrictEqual([
|
|
{ name: 'Alice', username: 'alice', id: 1 },
|
|
{ name: 'Almost Alice', username: 'almostalice', id: 4 },
|
|
]);
|
|
});
|
|
});
|