Use group factory functions in tests instead of normalizers
This commit is contained in:
parent
d747e323c6
commit
d12078a687
7 changed files with 45 additions and 23 deletions
|
@ -1,7 +1,7 @@
|
|||
import React from 'react';
|
||||
|
||||
import { buildGroup, buildGroupRelationship } from 'soapbox/jest/factory';
|
||||
import { render, screen } from 'soapbox/jest/test-helpers';
|
||||
import { normalizeGroup, normalizeGroupRelationship } from 'soapbox/normalizers';
|
||||
import { Group } from 'soapbox/types/entities';
|
||||
|
||||
import GroupActionButton from '../group-action-button';
|
||||
|
@ -11,7 +11,7 @@ let group: Group;
|
|||
describe('<GroupActionButton />', () => {
|
||||
describe('with no group relationship', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
group = buildGroup({
|
||||
relationship: null,
|
||||
});
|
||||
});
|
||||
|
@ -43,8 +43,8 @@ describe('<GroupActionButton />', () => {
|
|||
|
||||
describe('with no group relationship member', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
relationship: normalizeGroupRelationship({
|
||||
group = buildGroup({
|
||||
relationship: buildGroupRelationship({
|
||||
member: null,
|
||||
}),
|
||||
});
|
||||
|
@ -77,8 +77,8 @@ describe('<GroupActionButton />', () => {
|
|||
|
||||
describe('when the user has requested to join', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
relationship: normalizeGroupRelationship({
|
||||
group = buildGroup({
|
||||
relationship: buildGroupRelationship({
|
||||
requested: true,
|
||||
member: true,
|
||||
}),
|
||||
|
@ -94,8 +94,8 @@ describe('<GroupActionButton />', () => {
|
|||
|
||||
describe('when the user is an Admin', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
relationship: normalizeGroupRelationship({
|
||||
group = buildGroup({
|
||||
relationship: buildGroupRelationship({
|
||||
requested: false,
|
||||
member: true,
|
||||
role: 'admin',
|
||||
|
@ -112,8 +112,8 @@ describe('<GroupActionButton />', () => {
|
|||
|
||||
describe('when the user is just a member', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
relationship: normalizeGroupRelationship({
|
||||
group = buildGroup({
|
||||
relationship: buildGroupRelationship({
|
||||
requested: false,
|
||||
member: true,
|
||||
role: 'user',
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import React from 'react';
|
||||
|
||||
import { buildGroup } from 'soapbox/jest/factory';
|
||||
import { render, screen } from 'soapbox/jest/test-helpers';
|
||||
import { normalizeGroup } from 'soapbox/normalizers';
|
||||
import { Group } from 'soapbox/types/entities';
|
||||
|
||||
import GroupMemberCount from '../group-member-count';
|
||||
|
@ -12,7 +12,7 @@ describe('<GroupMemberCount />', () => {
|
|||
describe('with support for "members_count"', () => {
|
||||
describe('with 1 member', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
group = buildGroup({
|
||||
members_count: 1,
|
||||
});
|
||||
});
|
||||
|
@ -26,7 +26,7 @@ describe('<GroupMemberCount />', () => {
|
|||
|
||||
describe('with 2 members', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
group = buildGroup({
|
||||
members_count: 2,
|
||||
});
|
||||
});
|
||||
|
@ -40,7 +40,7 @@ describe('<GroupMemberCount />', () => {
|
|||
|
||||
describe('with 1000 members', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
group = buildGroup({
|
||||
members_count: 1000,
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import React from 'react';
|
||||
|
||||
import { buildGroup } from 'soapbox/jest/factory';
|
||||
import { render, screen } from 'soapbox/jest/test-helpers';
|
||||
import { normalizeGroup } from 'soapbox/normalizers';
|
||||
import { Group } from 'soapbox/types/entities';
|
||||
|
||||
import GroupPrivacy from '../group-privacy';
|
||||
|
@ -11,7 +11,7 @@ let group: Group;
|
|||
describe('<GroupPrivacy />', () => {
|
||||
describe('with a Private group', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
group = buildGroup({
|
||||
locked: true,
|
||||
});
|
||||
});
|
||||
|
@ -25,7 +25,7 @@ describe('<GroupPrivacy />', () => {
|
|||
|
||||
describe('with a Public group', () => {
|
||||
beforeEach(() => {
|
||||
group = normalizeGroup({
|
||||
group = buildGroup({
|
||||
locked: false,
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
import React from 'react';
|
||||
|
||||
import { __stub } from 'soapbox/api';
|
||||
import { buildGroup } from 'soapbox/jest/factory';
|
||||
import { render, screen, waitFor } from 'soapbox/jest/test-helpers';
|
||||
import { normalizeGroup, normalizeInstance } from 'soapbox/normalizers';
|
||||
import { normalizeInstance } from 'soapbox/normalizers';
|
||||
|
||||
import Search from '../search';
|
||||
|
||||
|
@ -35,7 +36,7 @@ describe('<Search />', () => {
|
|||
beforeEach(() => {
|
||||
__stub((mock) => {
|
||||
mock.onGet('/api/v1/groups/search').reply(200, [
|
||||
normalizeGroup({
|
||||
buildGroup({
|
||||
display_name: 'Group',
|
||||
id: '1',
|
||||
}),
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
import { Map as ImmutableMap } from 'immutable';
|
||||
|
||||
import { __stub } from 'soapbox/api';
|
||||
import { buildGroup, buildGroupRelationship } from 'soapbox/jest/factory';
|
||||
import { renderHook, waitFor } from 'soapbox/jest/test-helpers';
|
||||
import { normalizeAccount, normalizeGroup, normalizeGroupRelationship, normalizeInstance } from 'soapbox/normalizers';
|
||||
import { normalizeAccount, normalizeInstance } from 'soapbox/normalizers';
|
||||
|
||||
import { useGroupsPath } from '../useGroupsPath';
|
||||
|
||||
|
@ -53,14 +54,14 @@ describe('useGroupsPath()', () => {
|
|||
beforeEach(() => {
|
||||
__stub((mock) => {
|
||||
mock.onGet('/api/v1/groups').reply(200, [
|
||||
normalizeGroup({
|
||||
buildGroup({
|
||||
display_name: 'Group',
|
||||
id: '1',
|
||||
}),
|
||||
]);
|
||||
|
||||
mock.onGet('/api/v1/groups/relationships?id[]=1').reply(200, [
|
||||
normalizeGroupRelationship({
|
||||
buildGroupRelationship({
|
||||
id: '1',
|
||||
}),
|
||||
]);
|
||||
|
|
20
app/soapbox/jest/factory.ts
Normal file
20
app/soapbox/jest/factory.ts
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
import { groupSchema, Group, groupRelationshipSchema, GroupRelationship } from 'soapbox/schemas';
|
||||
|
||||
// TODO: there's probably a better way to create these factory functions.
|
||||
// This looks promising but didn't work on my first attempt: https://github.com/anatine/zod-plugins/tree/main/packages/zod-mock
|
||||
|
||||
function buildGroup(props: Record<string, any> = {}): Group {
|
||||
return groupSchema.parse(Object.assign({
|
||||
id: uuidv4(),
|
||||
}, props));
|
||||
}
|
||||
|
||||
function buildGroupRelationship(props: Record<string, any> = {}): GroupRelationship {
|
||||
return groupRelationshipSchema.parse(Object.assign({
|
||||
id: uuidv4(),
|
||||
}, props));
|
||||
}
|
||||
|
||||
export { buildGroup, buildGroupRelationship };
|
|
@ -21,7 +21,7 @@ const groupSchema = z.object({
|
|||
group_visibility: z.string().catch(''), // TruthSocial
|
||||
header: z.string().catch(headerMissing),
|
||||
header_static: z.string().catch(''),
|
||||
id: z.string().catch(''),
|
||||
id: z.string(),
|
||||
locked: z.boolean().catch(false),
|
||||
membership_required: z.boolean().catch(false),
|
||||
members_count: z.number().catch(0),
|
||||
|
|
Loading…
Reference in a new issue