File size: 5,344 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
//@flow
import { request } from '../../utils';
import db from 'shared/testing/db';
import data from 'shared/testing/data';
import { MAX_ID, BRYN_ID, DATE } from '../../../migrations/seed/default';
const generalChannelId = data.channels[0].id;
const owner = data.users.find(({ username }) => username === 'mxstbr');
const noPermissionUser = data.users.find(({ username }) => username === 'bryn');
const defaultChannelId = 'ce2b4488-4c75-47e0-8ebc-2539c';
const DEFAULT_CHANNELS = [
{
id: defaultChannelId,
communityId: 'ce2b4488-4c75-47e0-8ebc-2539c1e6a191',
createdAt: new Date(),
name: 'Default',
description: 'Default chatter',
slug: 'default',
isPrivate: false,
isDefault: true,
},
];
const DEFAULT_USERS_CHANNELS = [
{
createdAt: new Date(),
userId: MAX_ID,
channelId: defaultChannelId,
isOwner: true,
isModerator: false,
isMember: true,
isBlocked: false,
isPending: false,
receiveNotifications: true,
},
{
createdAt: new Date(),
userId: BRYN_ID,
channelId: defaultChannelId,
isOwner: false,
isModerator: false,
isMember: true,
isBlocked: false,
receiveNotifications: true,
},
];
const DEFAULT_THREADS = [
{
createdAt: new Date(DATE),
creatorId: MAX_ID,
channelId: defaultChannelId,
communityId: 'ce2b4488-4c75-47e0-8ebc-2539c1e6a191',
isPublished: true,
isLocked: false,
type: 'DRAFTJS',
content: {
title: 'The first thread! 🎉',
body: '',
},
edits: [],
modifiedAt: new Date(DATE),
lastActive: new Date(DATE),
},
{
createdAt: new Date(DATE + 1),
creatorId: MAX_ID,
channelId: defaultChannelId,
communityId: 'ce2b4488-4c75-47e0-8ebc-2539c1e6a191',
isPublished: true,
isLocked: false,
type: 'DRAFTJS',
content: {
title: 'Another thread',
body: '',
},
edits: [],
modifiedAt: new Date(DATE + 1),
lastActive: new Date(DATE + 1),
},
{
createdAt: new Date(DATE + 2),
creatorId: BRYN_ID,
channelId: defaultChannelId,
communityId: 'ce2b4488-4c75-47e0-8ebc-2539c1e6a191',
isPublished: true,
isLocked: false,
type: 'DRAFTJS',
content: {
title: 'Yet another thread',
body: '',
},
edits: [],
modifiedAt: new Date(DATE + 2),
lastActive: new Date(DATE + 2),
},
];
const cleanTables = () =>
Promise.all([
db
.table('channels')
.delete()
.run(),
db
.table('usersChannels')
.delete()
.run(),
db
.table('threads')
.delete()
.run(),
]);
const populateTables = () =>
Promise.all([
db
.table('channels')
.insert([...DEFAULT_CHANNELS, ...data.channels])
.run(),
db
.table('usersChannels')
.insert([...DEFAULT_USERS_CHANNELS, ...data.usersChannels])
.run(),
db
.table('threads')
.insert([...DEFAULT_THREADS, ...data.threads])
.run(),
]);
// after each test just rest the database
beforeEach(() => cleanTables().then(() => populateTables()));
afterEach(() => cleanTables().then(() => populateTables()));
const variables = {
channelId: defaultChannelId,
};
it('should delete a channel if user is owner', async () => {
const query = /* GraphQL */ `
mutation deleteChannel($channelId: ID!) {
deleteChannel(channelId: $channelId)
}
`;
const context = {
user: owner,
};
expect.assertions(1);
const result = await request(query, { context, variables });
expect(result).toMatchSnapshot();
});
it('should not delete a channel if user is not owner', async () => {
const query = /* GraphQL */ `
mutation deleteChannel($channelId: ID!) {
deleteChannel(channelId: $channelId)
}
`;
const context = {
user: noPermissionUser,
};
expect.assertions(1);
const result = await request(query, { context, variables });
expect(result).toMatchSnapshot();
});
it('should not delete a channel if user is not signed in', async () => {
const query = /* GraphQL */ `
mutation deleteChannel($channelId: ID!) {
deleteChannel(channelId: $channelId)
}
`;
expect.assertions(1);
const result = await request(query, { variables });
expect(result).toMatchSnapshot();
});
it('should not delete the general channel', async () => {
const query = /* GraphQL */ `
mutation deleteChannel($channelId: ID!) {
deleteChannel(channelId: $channelId)
}
`;
const context = {
user: owner,
};
expect.assertions(1);
const result = await request(query, {
context,
variables: {
channelId: generalChannelId,
},
});
expect(result).toMatchSnapshot();
});
it('should delete all threads in the deleted channel', async () => {
const getThreadsInChannel = () =>
db
.table('threads')
.filter({ channelId: defaultChannelId })
.run();
const query = /* GraphQL */ `
mutation deleteChannel($channelId: ID!) {
deleteChannel(channelId: $channelId)
}
`;
const context = {
user: owner,
};
expect.assertions(2);
const result = await request(query, { context, variables });
expect(result).toMatchSnapshot();
const threads = await getThreadsInChannel();
const filtered = threads.filter(t => t.deletedAt !== null).length;
expect(threads).toHaveLength(filtered);
});
|