Skip to content

Commit

Permalink
tests: make tests run
Browse files Browse the repository at this point in the history
  • Loading branch information
jankapunkt committed Apr 22, 2024
1 parent 01b77bf commit a415d28
Show file tree
Hide file tree
Showing 13 changed files with 634 additions and 594 deletions.
4 changes: 2 additions & 2 deletions tests/.meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ [email protected]
[email protected]
[email protected]
[email protected]
meteortesting:browser-tests@1.5.3
meteortesting:mocha@2.1.0
meteortesting:browser-tests@1.6.0-beta300.0
meteortesting:mocha@3.1.0-beta300.0
meteortesting:[email protected]
[email protected]
[email protected]
Expand Down
22 changes: 3 additions & 19 deletions tests/ajv.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,14 @@
import Ajv from 'ajv'
import expect from 'expect'
import { callMongoMethod } from './helper'
import { ajvImpl } from './libraries'

describe('using ajv', () => {
before(() => {
Collection2.defineValidation({
name: 'ajv',
is: schema => schema instanceof Ajv,
create: schema => {
const instance = new Ajv()
instance.definition = schema
return instance
},
extend: (s1, s2) => {
// not impl
return s2
},
clean: ({ doc, modifier, schema, userId, isLocalCollection, type }) => {
// not impl
},
validate: () => {},
freeze: false
});
Collection2.defineValidation(ajvImpl());
})

it('attach and get simpleSchema for normal collection', function () {
it('attach and get ajv for normal collection', function () {
;['ajvMc1', null].forEach(name => {
const mc = new Mongo.Collection(name, Meteor.isClient ? { connection: null } : undefined);

Expand Down
59 changes: 36 additions & 23 deletions tests/autoValue.tests.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
import 'meteor/aldeed:collection2/static';
import { Meteor } from 'meteor/meteor';
import expect from 'expect';
import { Mongo } from 'meteor/mongo';
import expect from 'expect';
import SimpleSchema from "meteor/aldeed:simple-schema";
import { callMongoMethod } from './helper';
import { Collection2 } from 'meteor/aldeed:collection2'
import { simpleSchemaImpl } from './libraries'

const collection = new Mongo.Collection('autoValueTestCollection');
const localCollection = new Mongo.Collection('autoValueTestLocalCollection', {
connection: null
});

[collection, localCollection].forEach((c) => {
c.attachSchema(
new SimpleSchema({
clientAV: {
type: SimpleSchema.Integer,
optional: true,
autoValue() {
if (Meteor.isServer) return;
return (this.value || 0) + 1;
}
},
serverAV: {
type: SimpleSchema.Integer,
optional: true,
autoValue() {
console.debug('get autovalues', Meteor.isClient)
if (Meteor.isClient) return;
return (this.value || 0) + 1;
const attach = () => {
[collection, localCollection].forEach((c) => {
c.attachSchema(
new SimpleSchema({
clientAV: {
type: SimpleSchema.Integer,
optional: true,
autoValue() {
if (Meteor.isServer) return;
return (this.value || 0) + 1;
}
},
serverAV: {
type: SimpleSchema.Integer,
optional: true,
autoValue() {
console.debug('get autovalues', Meteor.isClient)
if (Meteor.isClient) return;
return (this.value || 0) + 1;
}
}
}
})
);
});
})
);
});
}

if (Meteor.isClient) {
describe('autoValue on client', function () {
before(() => {
Collection2.defineValidation(simpleSchemaImpl());
attach()
})

it('for client insert, autoValues should be added on the server only (added to only a validated clone of the doc on client)', function (done) {
collection.insert({}, (error, id) => {
if (error) {
Expand Down Expand Up @@ -79,6 +88,10 @@ if (Meteor.isClient) {

if (Meteor.isServer) {
describe('autoValue on server', function () {
before(() => {
Collection2.defineValidation(simpleSchemaImpl())
attach()
})
it('runs function once', async function () {
const id = await callMongoMethod(collection, 'insert', [{}]);
const doc = await callMongoMethod(collection, 'findOne', [id]);
Expand Down
29 changes: 17 additions & 12 deletions tests/books.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import SimpleSchema from "meteor/aldeed:simple-schema";
import { Meteor } from 'meteor/meteor';
import { _ } from 'meteor/underscore';
import { callMeteorFetch, callMongoMethod } from './helper';
import { Collection2 } from 'meteor/aldeed:collection2'
import { simpleSchemaImpl } from './libraries'

/* global describe, it, beforeEach */

Expand Down Expand Up @@ -57,17 +59,20 @@ const booksSchema = new SimpleSchema({
});

const books = new Mongo.Collection('books');
books.attachSchema(booksSchema);

const upsertTest = new Mongo.Collection('upsertTest');
upsertTest.attachSchema(
new SimpleSchema({
_id: { type: String },
foo: { type: Number }

describe('SimpleSchema books tests', () => {
before(() => {
Collection2.defineValidation(simpleSchemaImpl())
books.attachSchema(booksSchema);
upsertTest.attachSchema(
new SimpleSchema({
_id: { type: String },
foo: { type: Number }
})
);
})
);

export default function addBooksTests() {
describe('insert', function () {
beforeEach(async function () {
for (const book of await callMeteorFetch(books, {})) {
Expand Down Expand Up @@ -380,7 +385,7 @@ export default function addBooksTests() {

if (Meteor.isServer) {
describe('upsert', function () {
function getCallback(done) {
function getCallback (done) {
return (result) => {
expect(result.numberAffected).toBe(1);

Expand All @@ -391,7 +396,7 @@ export default function addBooksTests() {
};
}

function getUpdateCallback(done) {
function getUpdateCallback (done) {
return (result) => {
expect(result).toBe(1);

Expand All @@ -402,7 +407,7 @@ export default function addBooksTests() {
};
}

function getErrorCallback(done) {
function getErrorCallback (done) {
return (error) => {
expect(!!error).toBe(true);
// expect(!!result).toBe(false)
Expand Down Expand Up @@ -737,4 +742,4 @@ export default function addBooksTests() {
expect(doc.foo).toBe(2);
});
}
}
});
9 changes: 7 additions & 2 deletions tests/clean.tests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-env mocha */
import expect from 'expect';
import { Mongo } from 'meteor/mongo';
import SimpleSchema from "meteor/aldeed:simple-schema";
import { Meteor } from 'meteor/meteor';
import { Collection2 } from 'meteor/aldeed:collection2'
import { callMongoMethod } from './helper';
import { simpleSchemaImpl } from './libraries'

/* global describe it */

let collection;

Expand All @@ -14,7 +16,10 @@ if (Meteor.isClient) {
collection = new Mongo.Collection('cleanTests');
}

describe('clean options', function () {
describe('SimpleSchema clean options', function () {
before(() => {
Collection2.defineValidation(simpleSchemaImpl())
})
describe('filter', function () {
it('keeps default schema clean options', function (done) {
const schema = new SimpleSchema(
Expand Down
14 changes: 6 additions & 8 deletions tests/collection2.tests.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import expect from 'expect';
import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'meteor/aldeed:simple-schema';
import addMultiTests from './multi.tests.js';
import addBooksTests from './books.tests.js';
import addContextTests from './context.tests.js';
import addDefaultValuesTests from './default.tests.js';
import { Meteor } from 'meteor/meteor';
import { callMongoMethod } from './helper';
import { Collection2 } from 'meteor/aldeed:collection2'
import { simpleSchemaImpl } from './libraries'

/* global describe, it */

describe('collection2', function () {
before(() => {
Collection2.defineValidation(simpleSchemaImpl())
})
it('attach and get simpleSchema for normal collection', function () {
const mc = new Mongo.Collection('mc', Meteor.isClient ? { connection: null } : undefined);

Expand Down Expand Up @@ -686,8 +687,5 @@ describe('collection2', function () {
}
});

addBooksTests();
addContextTests();
addDefaultValuesTests();
addMultiTests();

});
15 changes: 11 additions & 4 deletions tests/context.tests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-env mocha */
import expect from 'expect';
import { Mongo } from 'meteor/mongo';
import SimpleSchema from "meteor/aldeed:simple-schema";
import { Meteor } from 'meteor/meteor';
import { callMongoMethod } from './helper';
import { Collection2 } from 'meteor/aldeed:collection2'
import { simpleSchemaImpl } from './libraries'

/* global it */

const contextCheckSchema = new SimpleSchema({
foo: {
Expand Down Expand Up @@ -54,9 +56,14 @@ const contextCheckSchema = new SimpleSchema({
});

const contextCheck = new Mongo.Collection('contextCheck');
contextCheck.attachSchema(contextCheckSchema);

export default function addContextTests() {

describe('context tests', () => {
before(() => {
Collection2.defineValidation(simpleSchemaImpl())
contextCheck.attachSchema(contextCheckSchema);
})

it('AutoValue Context', async function () {
const testId = await callMongoMethod(contextCheck, 'insert', [{}]);

Expand Down Expand Up @@ -101,4 +108,4 @@ export default function addContextTests() {
ctx = await callMongoMethod(contextCheck, 'findOne', [testId]);
expect(ctx.context.docId).toBe(testId);
});
}
});
15 changes: 10 additions & 5 deletions tests/default.tests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-env mocha */
import expect from 'expect';
import { Mongo } from 'meteor/mongo';
import SimpleSchema from "meteor/aldeed:simple-schema";
import { Meteor } from 'meteor/meteor';
import { callMongoMethod } from './helper';

/* global it */
import { simpleSchemaImpl } from './libraries'
import { Collection2 } from 'meteor/aldeed:collection2'

const defaultValuesSchema = new SimpleSchema({
bool1: {
Expand All @@ -14,10 +15,14 @@ const defaultValuesSchema = new SimpleSchema({
});

const defaultValues = new Mongo.Collection('dv');
defaultValues.attachSchema(defaultValuesSchema);
global.defaultValues = defaultValues;

export default function addDefaultValuesTests() {
describe('defaults tests', () => {
before(() => {
Collection2.defineValidation(simpleSchemaImpl())
defaultValues.attachSchema(defaultValuesSchema);
})

if (Meteor.isServer) {
it('defaultValues', function (done) {
let p;
Expand Down Expand Up @@ -81,4 +86,4 @@ export default function addDefaultValuesTests() {
expect(p.bool1).toBe(true);
});
}
}
});
65 changes: 65 additions & 0 deletions tests/libraries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Ajv from 'ajv'
import SimpleSchema from 'meteor/aldeed:simple-schema'
import { Collection2 } from 'meteor/aldeed:collection2'
export const ajvImpl = () => ({
name: 'ajv',
is: schema => schema instanceof Ajv,
create: schema => {
const instance = new Ajv()
instance.definition = schema
return instance
},
extend: (s1, s2) => {
// not impl
return s2
},
clean: ({ doc, modifier, schema, userId, isLocalCollection, type }) => {
// not impl
},
validate: () => {},
freeze: false
})

export const simpleSchemaImpl = () => ({
name: 'SimpleSchema',
is: schema => SimpleSchema.isSimpleSchema(schema),
create: schema => new SimpleSchema(schema),
extend: (s1, s2) => {
if (s2.version >= 2) {
const ss = new SimpleSchema(s1);
ss.extend(s2);
return ss;
} else {
return new SimpleSchema([s1, s2]);
}
},
clean: ({ doc, modifier, schema, userId, isLocalCollection, type }) => {
const isModifier = !Collection2.isInsertType(type);
const target = isModifier ? modifier : doc;
schema.clean(target, {
mutate: true,
isModifier,
// We don't do these here because they are done on the client if desired
filter: false,
autoConvert: false,
removeEmptyStrings: false,
trimStrings: false,
extendAutoValueContext: {
isInsert: Collection2.isInsertType(type),
isUpdate: Collection2.isUpdateType(type),
isUpsert: Collection2.isUpdateType(type),
userId,
isFromTrustedCode: false,
docId: doc?._id,
isLocalCollection
}
})
},
validate: ({}) => {

},
getErrors: () => {

},
freeze: false
})
Loading

0 comments on commit a415d28

Please sign in to comment.