|
| 1 | +jest.setTimeout(120000); |
| 2 | + |
| 3 | +import { |
| 4 | + alpha, |
| 5 | + setupLogins, |
| 6 | + createCommunity, |
| 7 | + unfollows, |
| 8 | + randomString, |
| 9 | + createPost, |
| 10 | +} from "./shared"; |
| 11 | +import { CreateCommunityTag } from "lemmy-js-client/dist/types/CreateCommunityTag"; |
| 12 | +import { UpdateCommunityTag } from "lemmy-js-client/dist/types/UpdateCommunityTag"; |
| 13 | +import { DeleteCommunityTag } from "lemmy-js-client/dist/types/DeleteCommunityTag"; |
| 14 | +import { EditPost } from "lemmy-js-client"; |
| 15 | + |
| 16 | +beforeAll(setupLogins); |
| 17 | +afterAll(unfollows); |
| 18 | + |
| 19 | +test("Create, update, delete community tag", async () => { |
| 20 | + // Create a community first |
| 21 | + let communityRes = await createCommunity(alpha); |
| 22 | + const communityId = communityRes.community_view.community.id; |
| 23 | + |
| 24 | + // Create a tag |
| 25 | + const tagName = randomString(10); |
| 26 | + let createForm: CreateCommunityTag = { |
| 27 | + display_name: tagName, |
| 28 | + community_id: communityId, |
| 29 | + }; |
| 30 | + let createRes = await alpha.createCommunityTag(createForm); |
| 31 | + expect(createRes.id).toBeDefined(); |
| 32 | + expect(createRes.display_name).toBe(tagName); |
| 33 | + expect(createRes.community_id).toBe(communityId); |
| 34 | + |
| 35 | + // Update the tag |
| 36 | + const newTagName = randomString(10); |
| 37 | + let updateForm: UpdateCommunityTag = { |
| 38 | + tag_id: createRes.id, |
| 39 | + display_name: newTagName, |
| 40 | + }; |
| 41 | + let updateRes = await alpha.updateCommunityTag(updateForm); |
| 42 | + expect(updateRes.id).toBe(createRes.id); |
| 43 | + expect(updateRes.display_name).toBe(newTagName); |
| 44 | + expect(updateRes.community_id).toBe(communityId); |
| 45 | + |
| 46 | + // List tags |
| 47 | + let listRes = await alpha.getCommunity({ id: communityId }); |
| 48 | + expect(listRes.community_view.post_tags.length).toBe(1); |
| 49 | + expect( |
| 50 | + listRes.community_view.post_tags.find(t => t.id === createRes.id) |
| 51 | + ?.display_name, |
| 52 | + ).toBe(newTagName); |
| 53 | + |
| 54 | + // Delete the tag |
| 55 | + let deleteForm: DeleteCommunityTag = { |
| 56 | + tag_id: createRes.id, |
| 57 | + }; |
| 58 | + let deleteRes = await alpha.deleteCommunityTag(deleteForm); |
| 59 | + expect(deleteRes.id).toBe(createRes.id); |
| 60 | + |
| 61 | + // Verify tag is deleted |
| 62 | + listRes = await alpha.getCommunity({ id: communityId }); |
| 63 | + expect( |
| 64 | + listRes.community_view.post_tags.find(t => t.id === createRes.id), |
| 65 | + ).toBeUndefined(); |
| 66 | + expect(listRes.community_view.post_tags.length).toBe(0); |
| 67 | +}); |
| 68 | + |
| 69 | +test("Update post tags", async () => { |
| 70 | + // Create a community |
| 71 | + let communityRes = await createCommunity(alpha); |
| 72 | + const communityId = communityRes.community_view.community.id; |
| 73 | + |
| 74 | + // Create two tags |
| 75 | + const tag1Name = randomString(10); |
| 76 | + let createForm1: CreateCommunityTag = { |
| 77 | + display_name: tag1Name, |
| 78 | + community_id: communityId, |
| 79 | + }; |
| 80 | + let tag1Res = await alpha.createCommunityTag(createForm1); |
| 81 | + expect(tag1Res.id).toBeDefined(); |
| 82 | + |
| 83 | + const tag2Name = randomString(10); |
| 84 | + let createForm2: CreateCommunityTag = { |
| 85 | + display_name: tag2Name, |
| 86 | + community_id: communityId, |
| 87 | + }; |
| 88 | + let tag2Res = await alpha.createCommunityTag(createForm2); |
| 89 | + expect(tag2Res.id).toBeDefined(); |
| 90 | + |
| 91 | + // Create a post |
| 92 | + let postRes = await alpha.createPost({ |
| 93 | + name: randomString(10), |
| 94 | + community_id: communityId, |
| 95 | + }); |
| 96 | + expect(postRes.post_view.post.id).toBeDefined(); |
| 97 | + |
| 98 | + // Update post tags |
| 99 | + let updateForm: EditPost = { |
| 100 | + post_id: postRes.post_view.post.id, |
| 101 | + tags: [tag1Res.id, tag2Res.id], |
| 102 | + }; |
| 103 | + let updateRes = await alpha.editPost(updateForm); |
| 104 | + expect(updateRes.post_view.post.id).toBe(postRes.post_view.post.id); |
| 105 | + expect(updateRes.post_view.tags?.length).toBe(2); |
| 106 | + expect(updateRes.post_view.tags?.map(t => t.id).sort()).toEqual( |
| 107 | + [tag1Res.id, tag2Res.id].sort(), |
| 108 | + ); |
| 109 | + |
| 110 | + // Update post to remove one tag |
| 111 | + updateForm.tags = [tag1Res.id]; |
| 112 | + updateRes = await alpha.editPost(updateForm); |
| 113 | + expect(updateRes.post_view.post.id).toBe(postRes.post_view.post.id); |
| 114 | + expect(updateRes.post_view.tags?.length).toBe(1); |
| 115 | + expect(updateRes.post_view.tags?.[0].id).toBe(tag1Res.id); |
| 116 | +}); |
| 117 | + |
| 118 | +test("Post author can update post tags", async () => { |
| 119 | + // Create a community |
| 120 | + let communityRes = await createCommunity(alpha); |
| 121 | + const communityId = communityRes.community_view.community.id; |
| 122 | + |
| 123 | + // Create a tag |
| 124 | + const tagName = randomString(10); |
| 125 | + let createForm: CreateCommunityTag = { |
| 126 | + display_name: tagName, |
| 127 | + community_id: communityId, |
| 128 | + }; |
| 129 | + let tagRes = await alpha.createCommunityTag(createForm); |
| 130 | + expect(tagRes.id).toBeDefined(); |
| 131 | + |
| 132 | + let postRes = await createPost( |
| 133 | + alpha, |
| 134 | + communityId, |
| 135 | + "https://example.com/", |
| 136 | + "post with tags", |
| 137 | + ); |
| 138 | + expect(postRes.post_view.post.id).toBeDefined(); |
| 139 | + |
| 140 | + // Alpha should be able to update tags on their own post |
| 141 | + let updateForm: EditPost = { |
| 142 | + post_id: postRes.post_view.post.id, |
| 143 | + tags: [tagRes.id], |
| 144 | + }; |
| 145 | + let updateRes = await alpha.editPost(updateForm); |
| 146 | + expect(updateRes.post_view.post.id).toBe(postRes.post_view.post.id); |
| 147 | + expect(updateRes.post_view.tags?.length).toBe(1); |
| 148 | + expect(updateRes.post_view.tags?.[0].id).toBe(tagRes.id); |
| 149 | +}); |
0 commit comments