File size: 2,324 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
//@flow
import { request } from '../../utils';
import db from 'shared/testing/db';
import data from 'shared/testing/data';

const defaultThread = data.threads[0];
const owner = data.users.find(({ username }) => username === 'mxstbr');
const member = data.users.find(({ username }) => username === 'brian');
const noPermissionUser = data.users.find(
  ({ username }) => username === 'bad-boy'
);

// before each test, makre sure the thread exists to test deletion
beforeEach(async () => {
  const threadExists = await db
    .table('threads')
    .get(defaultThread.id)
    .run();

  if (threadExists.deletedAt) {
    return db
      .table('threads')
      .get(defaultThread.id)
      .update({
        deletedAt: db.literal(),
      })
      .run();
  }
});

const variables = {
  threadId: defaultThread.id,
};

it('should be able to delete self-published thread', async () => {
  const query = /* GraphQL */ `
    mutation deleteThread($threadId: ID!) {
      deleteThread (threadId: $threadId)
    },
  `;

  const context = {
    user: member,
  };

  expect.assertions(1);

  const result = await request(query, { context, variables });
  expect(result).toMatchSnapshot();
});

it('should be able to delete thread if user owns community', async () => {
  const query = /* GraphQL */ `
    mutation deleteThread($threadId: ID!) {
      deleteThread (threadId: $threadId)
    },
  `;

  const context = {
    user: owner,
  };

  expect.assertions(1);

  const result = await request(query, { context, variables });
  expect(result).toMatchSnapshot();
});

it("should not delete thread if user doesn't have permissions", async () => {
  const query = /* GraphQL */ `
    mutation deleteThread($threadId: ID!) {
      deleteThread (threadId: $threadId)
    },
  `;

  const context = {
    user: noPermissionUser,
  };

  expect.assertions(1);

  const result = await request(query, { context, variables });
  expect(result).toMatchSnapshot();
});

it('should not delete thread if user is not signed in', async () => {
  const query = /* GraphQL */ `
    mutation deleteThread($threadId: ID!) {
      deleteThread (threadId: $threadId)
    },
  `;

  const context = {
    user: null,
  };

  expect.assertions(1);

  const result = await request(query, { context, variables });
  expect(result).toMatchSnapshot();
});