-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmissions-service.test.ts
More file actions
123 lines (103 loc) · 3.96 KB
/
submissions-service.test.ts
File metadata and controls
123 lines (103 loc) · 3.96 KB
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
import {
ChallengesService,
SubmissionService,
} from "../../../app/challenges-platform";
import { uuid } from "../../../app/common";
import { accessibleChallengeFactory } from "../factories/accessible-challenge-factory";
import { challengeFactory } from "../factories/challenge-factory";
import { participantFactory } from "../factories/participant-factory";
describe("SubmissionService", () => {
describe("findByUuid", () => {
describe("when the id is invalid", () => {
it("returns an error", async () => {
const result = await SubmissionService.findByUuid("invalid-id");
expect(result.err).toBe(true);
expect(result.val.toString()).toBe("Error: Invalid UUID");
});
});
describe("when there is an existing record", () => {
it("returns the submission", async () => {
const challenge = await challengeFactory();
const participant = await participantFactory();
const insert = await accessibleChallengeFactory({
challenge,
participant,
});
const submission = await SubmissionService.create(
challenge.uuid,
participant.uuid,
);
if (!submission.ok) fail("Expected submission to be Ok");
const result = await SubmissionService.findByUuid(submission.val.uuid);
if (!result.ok) fail("Expected result to be Ok");
expect(result.val.challenge.id).toBe(challenge.id);
expect(result.val.participant.id).toBe(participant.id);
});
});
describe("when there is no record", () => {
it("returns an error", async () => {
const testUuid = uuid.create();
const result = await SubmissionService.findByUuid(testUuid);
expect(result.err).toBe(true);
expect(result.val.toString()).toBe("Error: Submission not found");
});
});
});
describe("create", () => {
describe("when challenge and participant exist", () => {
it("succesfully creates a submission", async () => {
const challenge = await challengeFactory();
const participant = await participantFactory();
const insert = await accessibleChallengeFactory({
challenge,
participant,
});
const result = await SubmissionService.create(
challenge.uuid,
participant.uuid,
);
if (!result.ok) fail("Expected result to be Ok");
expect(result.val.challenge.id).toBe(challenge.id);
expect(result.val.participant.id).toBe(participant.id);
});
});
describe("when challenge does not exist", () => {
it("returns an error", async () => {
const participant = await participantFactory();
const result = await SubmissionService.create(
"invalid-uuid",
participant.uuid,
);
expect(result.err).toBe(true);
expect(result.val.toString()).toBe("Error: Failed to find challenge");
});
});
describe("when challenge is deleted", () => {
it("returns an error", async () => {
const factory = await challengeFactory();
// Delete the challenge
const challenge = await ChallengesService.destroy(factory.uuid);
if (!challenge.ok)
fail("Expected challenge to be Ok, something went really wrong");
const participant = await participantFactory();
const result = await SubmissionService.create(
challenge.val.uuid,
participant.uuid,
);
expect(result.err).toBe(true);
expect(result.val.toString()).toBe("Error: Challenge is deleted");
});
});
describe("when participant does not exist", () => {
it("returns an error", async () => {
const challenge = await challengeFactory();
const result = await SubmissionService.create(
challenge.uuid,
"invalid-uuid",
);
expect(result.err).toBe(true);
expect(result.val.toString()).toBe("Error: Failed to find participant");
});
});
});
});