aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/event.model.test.ts34
-rw-r--r--test/model.ts16
-rw-r--r--test/utils/dbConnection.ts26
3 files changed, 76 insertions, 0 deletions
diff --git a/test/event.model.test.ts b/test/event.model.test.ts
new file mode 100644
index 0000000..05afb7a
--- /dev/null
+++ b/test/event.model.test.ts
@@ -0,0 +1,34 @@
+import { expect } from 'chai';
+import Model from './model';
+
+describe('Event model', () => {
+ it('Should assign status and nextRunAt on creation', async () => {
+ const event = await Model.create({
+ type: 'test',
+ schedule: '* * * * *',
+ context: {
+ message: 'Hello, world!'
+ },
+ });
+ expect(event.status).equal('notStarted');
+ expect(event).to.have.property('nextRunAt');
+ });
+
+ describe('Methods', () => {
+ it('log', async () => {
+ const event = await Model.findOne();
+ const log = await event?.log('Test message');
+ expect(log).to.have.property('message').equal('Test message');
+ });
+
+ it('start');
+
+ it('complete');
+
+ it('fail');
+
+ it('getLogs')
+
+ it('computeNextRunAt');
+ })
+});
diff --git a/test/model.ts b/test/model.ts
new file mode 100644
index 0000000..4d189dc
--- /dev/null
+++ b/test/model.ts
@@ -0,0 +1,16 @@
+import { Schema } from 'mongoose';
+import model from '../lib/event.model';
+
+interface Context {
+ message: string;
+}
+
+const contextSchema = new Schema({
+ message: String
+});
+
+const CustomEventModel = model<Context>('CustomEvent', contextSchema);
+
+
+export default CustomEventModel;
+
diff --git a/test/utils/dbConnection.ts b/test/utils/dbConnection.ts
new file mode 100644
index 0000000..ee1e9e0
--- /dev/null
+++ b/test/utils/dbConnection.ts
@@ -0,0 +1,26 @@
+import mongoose from 'mongoose';
+import Promise from 'bluebird';
+
+mongoose.Promise = Promise;
+
+const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/mongo-cronjob-test';
+const { MONGODB_USER, MONGODB_PASSWORD } = process.env;
+
+mongoose.connect(MONGODB_URL, {
+ user: MONGODB_USER,
+ pass: MONGODB_PASSWORD,
+ useNewUrlParser: true,
+ useUnifiedTopology: true,
+ useCreateIndex: true,
+ useFindAndModify: false,
+ family: 4 // Use IPv4, skip trying IPv6
+});
+
+const db = mongoose.connection;
+db.on('error', console.error.bind(console, 'connection error:'));
+db.once('open', () => {
+ console.log('Connection to MongoDB successful');
+});
+
+export default db;
+