Skip to content

Commit 5937452

Browse files
committed
* Update tests to add new configSchemaValidation tests
1 parent 7bc547e commit 5937452

File tree

4 files changed

+58
-59
lines changed

4 files changed

+58
-59
lines changed

package.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Package.onTest(function (api) {
2020

2121
api.addFiles([
2222
"tests/basic.js",
23+
"tests/configSchemaValidation.js",
2324
"tests/internals.js",
2425
"tests/replication.js"
25-
])
26+
]);
2627
});

server/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const JobRunConfigSchema = {
4646
unique: Match.Maybe(Boolean),
4747
singular: Match.Maybe(Boolean),
4848
callback: Match.Maybe(Function),
49+
data: Match.Maybe(Object),
4950
};
5051

5152
// Configure the package (optional)

tests/basic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Tinytest.addAsync("Basic", async function (test) {
1414

1515
console.log("--- 0 ---")
1616
var clear = await Jobs.clear("*")
17-
test.equal(clear, 1);
17+
test.equal(clear, 2);
1818

1919
// 1 - Register the Job
2020

tests/configSchemaValidation.js

Lines changed: 54 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
JobsTests4 = function () {
1+
import { Jobs, JobsInternal } from 'meteor/msavin:sjobs';
2+
import { Tinytest } from 'meteor/tinytest';
23

4+
Tinytest.addAsync('Config Schema Validations', async function (test) {
35
// 0 - Clear the collection
4-
console.log("--- 0 ---")
5-
var clear = Jobs.clear("*")
6-
console.log(clear)
6+
console.log("--- 0 ---");
7+
var clear = await Jobs.clear("*");
78

89
// 1 - Register a Job
9-
console.log("--- 1 ---")
10+
console.log("--- 1 ---");
1011
Jobs.register({
1112
"sendReminder": function (email, message) {
1213
console.log("Sending reminder to " + email + " with message: " + message);
@@ -16,112 +17,108 @@ JobsTests4 = function () {
1617
console.log("Running simple job with no arguments.");
1718
this.success();
1819
}
19-
})
20+
});
2021

2122
// 2 - Schedule a job with valid config
22-
console.log("--- 2 ---")
23+
console.log("--- 2 ---");
24+
let jobId1;
2325
try {
24-
var jobId1 = Jobs.run("sendReminder", "[email protected]", "The future is here!", {
25-
in: {
26-
days: 3
27-
},
26+
jobId1 = await Jobs.run("sendReminder", "[email protected]", "The future is here!", {
27+
in: { days: 3 },
2828
priority: 999,
2929
singular: true,
3030
callback: function () {
3131
console.log("Callback executed successfully for jobId1");
3232
}
33-
})
34-
console.log("JobId1 created successfully:", jobId1._id)
33+
});
34+
console.log("JobId1 created successfully:", jobId1._id);
3535
} catch (e) {
3636
console.error("Error scheduling job with valid config:", e.message);
3737
}
3838

39+
// Verify that the job was created
40+
test.isNotNull(jobId1, "JobId1 should be created successfully");
41+
3942
// 3 - Schedule a job with only the job name (no config or additional arguments)
40-
console.log("--- 3 ---")
43+
console.log("--- 3 ---");
44+
let jobId2;
4145
try {
42-
var jobId2 = Jobs.run("simpleJob");
43-
console.log("JobId2 created successfully:", jobId2._id)
46+
jobId2 = await Jobs.run("simpleJob");
47+
console.log("JobId2 created successfully:", jobId2._id);
4448
} catch (e) {
4549
console.error("Error scheduling simple job:", e.message);
4650
}
4751

52+
// Verify that the job was created
53+
test.isNotNull(jobId2, "JobId2 should be created successfully");
54+
4855
// 4 - Schedule a job with invalid config (typo in 'priority')
49-
console.log("--- 4 ---")
56+
console.log("--- 4 ---");
5057
try {
51-
var jobId3 = Jobs.run("sendReminder", "[email protected]", "Hello again!", {
52-
in: {
53-
days: 1
54-
},
58+
await Jobs.run("sendReminder", "[email protected]", "Hello again!", {
59+
in: { days: 1 },
5560
prioirty: 1000, // Intentional typo: should be 'priority'
5661
singular: true,
5762
callback: function () {
5863
console.log("This callback should not be executed for jobId3");
5964
}
60-
})
61-
console.error("JobId3 should not be created due to invalid config.");
65+
});
66+
test.fail("JobId3 should not be created due to invalid config.");
6267
} catch (e) {
6368
console.log("Caught expected error for invalid config:", e.message);
69+
test.ok("Caught expected error for invalid config");
6470
}
6571

6672
// 5 - Schedule a job with an unsupported config key
67-
console.log("--- 5 ---")
73+
console.log("--- 5 ---");
6874
try {
69-
var jobId4 = Jobs.run("sendReminder", "[email protected]", "Reminder!", {
70-
in: {
71-
hours: 2
72-
},
75+
await Jobs.run("sendReminder", "[email protected]", "Reminder!", {
76+
in: { hours: 2 },
7377
singular: true,
7478
unsupportedKey: "this should fail" // Unsupported key
75-
})
76-
console.error("JobId4 should not be created due to unsupported config key.");
79+
});
80+
test.fail("JobId4 should not be created due to unsupported config key.");
7781
} catch (e) {
7882
console.log("Caught expected error for unsupported config key:", e.message);
83+
test.ok("Caught expected error for unsupported config key");
7984
}
8085

8186
// 6 - Log whatever is in the collection
82-
console.log("--- 6 ---")
83-
var allJobDocs = JobsInternal.Utilities.collection.find().fetch();
87+
console.log("--- 6 ---");
88+
var allJobDocs = await JobsInternal.Utilities.collection.find().fetchAsync();
8489
console.log(allJobDocs);
8590

8691
// 7 - Cancel the successfully created job (jobId1)
87-
console.log("--- 7 ---")
92+
console.log("--- 7 ---");
8893
if (jobId1 && jobId1._id) {
89-
var cancel = Jobs.cancel(jobId1._id);
94+
var cancel = await Jobs.cancel(jobId1._id);
9095
console.log("Cancelled jobId1:", cancel);
9196

9297
// 8 - Verify the job was cancelled
93-
var jobDoc = Jobs.get(jobId1._id);
94-
console.log("--- 8 ---")
95-
console.log("Job doc after cancel:")
96-
console.log(jobDoc)
98+
var jobDoc = await Jobs.get(jobId1._id);
99+
console.log("--- 8 ---");
100+
console.log("Job doc after cancel:");
101+
console.log(jobDoc);
97102

98-
if (jobDoc.state === "cancelled") {
99-
console.log("Job was successfully cancelled")
100-
} else {
101-
console.log("Job cancel failed")
102-
}
103+
test.equal(jobDoc.state, "cancelled", "Job should be successfully cancelled");
103104
} else {
104-
console.error("jobId1 was not created successfully, skipping cancel test.");
105+
test.fail("jobId1 was not created successfully, skipping cancel test.");
105106
}
106107

107108
// 9 - Try to cancel a job that was created without any config or arguments (jobId2)
108-
console.log("--- 9 ---")
109+
console.log("--- 9 ---");
109110
if (jobId2 && jobId2._id) {
110-
var cancel2 = Jobs.cancel(jobId2._id);
111+
var cancel2 = await Jobs.cancel(jobId2._id);
111112
console.log("Cancelled jobId2:", cancel2);
112113

113114
// 10 - Verify the job was cancelled
114-
var jobDoc2 = Jobs.get(jobId2._id);
115-
console.log("--- 10 ---")
116-
console.log("Job doc after cancel:")
117-
console.log(jobDoc2)
115+
var jobDoc2 = await Jobs.get(jobId2._id);
116+
console.log("--- 10 ---");
117+
console.log("Job doc after cancel:");
118+
console.log(jobDoc2);
118119

119-
if (jobDoc2.state === "cancelled") {
120-
console.log("Job was successfully cancelled")
121-
} else {
122-
console.log("Job cancel failed")
123-
}
120+
test.equal(jobDoc2.state, "cancelled", "Job should be successfully cancelled");
124121
} else {
125-
console.error("jobId2 was not created successfully, skipping cancel test.");
122+
test.fail("jobId2 was not created successfully, skipping cancel test.");
126123
}
127-
}
124+
});

0 commit comments

Comments
 (0)