Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Apr 11, 2020
1 parent 80f84f0 commit 2f73e1b
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 24 deletions.
33 changes: 33 additions & 0 deletions app/settings/server/functions/settings.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import mock from 'mock-require';

type Dictionary = {
[index: string]: any;
}

class SettingsClass {
public data = new Map<string, Dictionary>()

public upsertCalls = 0;

findOne(query: Dictionary): any {
return [...this.data.values()].find((data) => Object.entries(query).every(([key, value]) => data[key] === value));
}

upsert(query: any, update: any): void {
const existent = this.findOne(query);

const data = { ...existent, ...query, ...update.$set };

if (!existent) {
Object.assign(data, update.$setOnInsert);
}

// console.log(query, data);
this.data.set(query._id, data);
this.upsertCalls++;
}
}

export const Settings = new SettingsClass();

mock('../../../models/server/models/Settings', Settings);
206 changes: 206 additions & 0 deletions app/settings/server/functions/settings.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/* eslint-disable @typescript-eslint/camelcase */
/* eslint-env mocha */
import { Meteor } from 'meteor/meteor';
import { expect } from 'chai';

import { Settings } from './settings.mocks';
import { settings } from './settings';

describe('Settings', () => {
beforeEach(() => {
Settings.upsertCalls = 0;
Settings.data.clear();
Meteor.settings = { public: {} };
process.env = {};
});

it('should not insert the same setting twice', () => {
settings.addGroup('group', function() {
this.section('section', function() {
this.add('my_setting', true, {
type: 'boolean',
sorter: 0,
});
});
});

expect(Settings.data.size).to.be.equal(2);
expect(Settings.upsertCalls).to.be.equal(2);
expect(Settings.findOne({ _id: 'my_setting' })).to.be.include({
type: 'boolean',
sorter: 0,
group: 'group',
section: 'section',
packageValue: true,
value: true,
valueSource: 'packageValue',
hidden: false,
blocked: false,
secret: false,
i18nLabel: 'my_setting',
i18nDescription: 'my_setting_Description',
autocomplete: true,
});

settings.addGroup('group', function() {
this.section('section', function() {
this.add('my_setting', true, {
type: 'boolean',
sorter: 0,
});
});
});

expect(Settings.data.size).to.be.equal(2);
expect(Settings.upsertCalls).to.be.equal(2);
expect(Settings.findOne({ _id: 'my_setting' }).value).to.be.equal(true);

settings.addGroup('group', function() {
this.section('section', function() {
this.add('my_setting2', false, {
type: 'boolean',
sorter: 0,
});
});
});

expect(Settings.data.size).to.be.equal(3);
expect(Settings.upsertCalls).to.be.equal(3);
expect(Settings.findOne({ _id: 'my_setting' }).value).to.be.equal(true);
expect(Settings.findOne({ _id: 'my_setting2' }).value).to.be.equal(false);
});

it('should respect override via environment', () => {
process.env.OVERWRITE_SETTING_my_setting = 'false';

settings.addGroup('group', function() {
this.section('section', function() {
this.add('my_setting', true, {
type: 'boolean',
sorter: 0,
});
});
});

expect(Settings.data.size).to.be.equal(2);
expect(Settings.upsertCalls).to.be.equal(2);
expect(Settings.findOne({ _id: 'my_setting' })).to.include({
value: false,
processEnvValue: false,
valueSource: 'processEnvValue',
type: 'boolean',
sorter: 0,
group: 'group',
section: 'section',
packageValue: true,
hidden: false,
blocked: false,
secret: false,
i18nLabel: 'my_setting',
i18nDescription: 'my_setting_Description',
autocomplete: true,
});
});

it('should respect initial value via environment', () => {
process.env.my_setting = '1';

settings.addGroup('group', function() {
this.section('section', function() {
this.add('my_setting', 0, {
type: 'int',
sorter: 0,
});
});
});

const expectedSetting = {
value: 1,
processEnvValue: 1,
valueSource: 'processEnvValue',
type: 'int',
sorter: 0,
group: 'group',
section: 'section',
packageValue: 0,
hidden: false,
blocked: false,
secret: false,
i18nLabel: 'my_setting',
i18nDescription: 'my_setting_Description',
autocomplete: true,
};

expect(Settings.data.size).to.be.equal(2);
expect(Settings.upsertCalls).to.be.equal(2);
expect(Settings.findOne({ _id: 'my_setting' })).to.include(expectedSetting);

process.env.my_setting = '2';

settings.addGroup('group', function() {
this.section('section', function() {
this.add('my_setting', 0, {
type: 'int',
sorter: 0,
});
});
});

expectedSetting.processEnvValue = 2;

expect(Settings.data.size).to.be.equal(2);
expect(Settings.upsertCalls).to.be.equal(3);
expect(Settings.findOne({ _id: 'my_setting' })).to.include(expectedSetting);
});

it('should respect initial value via Meteor.settings', () => {
Meteor.settings.my_setting = 1;

settings.addGroup('group', function() {
this.section('section', function() {
this.add('my_setting', 0, {
type: 'int',
sorter: 0,
});
});
});

const expectedSetting = {
value: 1,
meteorSettingsValue: 1,
valueSource: 'meteorSettingsValue',
type: 'int',
sorter: 0,
group: 'group',
section: 'section',
packageValue: 0,
hidden: false,
blocked: false,
secret: false,
i18nLabel: 'my_setting',
i18nDescription: 'my_setting_Description',
autocomplete: true,
};

expect(Settings.data.size).to.be.equal(2);
expect(Settings.upsertCalls).to.be.equal(2);
expect(Settings.findOne({ _id: 'my_setting' })).to.include(expectedSetting);

Meteor.settings.my_setting = 2;

settings.addGroup('group', function() {
this.section('section', function() {
this.add('my_setting', 0, {
type: 'int',
sorter: 0,
});
});
});

expectedSetting.meteorSettingsValue = 2;

expect(Settings.data.size).to.be.equal(2);
expect(Settings.upsertCalls).to.be.equal(3);
expect(Settings.findOne({ _id: 'my_setting' })).to.include(expectedSetting);
});
});
27 changes: 18 additions & 9 deletions app/settings/server/functions/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ const overrideSetting = (_id: string, value: SettingValue, options: ISettingAddO
options.valueSource = 'meteorSettingsValue';
}

const meteorValue = process?.env?.[`OVERWRITE_SETTING_${ _id }`];
if (meteorValue) {
if (meteorValue.toLowerCase() === 'true') {
const overwriteValue = process?.env?.[`OVERWRITE_SETTING_${ _id }`];
if (overwriteValue) {
if (overwriteValue.toLowerCase() === 'true') {
value = true;
} else if (meteorValue.toLowerCase() === 'false') {
} else if (overwriteValue.toLowerCase() === 'false') {
value = false;
} else if (options.type === 'int') {
value = parseInt(meteorValue);
value = parseInt(overwriteValue);
}
options.value = value;
options.processEnvValue = value;
Expand Down Expand Up @@ -104,6 +104,15 @@ type Query<T> = {
[P in keyof T]?: T[P] | QueryExpression;
}

type addSectionCallback = (this: {
add(id: string, value: SettingValue, options: ISettingAddOptions): void;
}) => void;

type addGroupCallback = (this: {
add(id: string, value: SettingValue, options: ISettingAddOptions): void;
section(section: string, cb: addSectionCallback): void;
}) => void;

class Settings extends SettingsBase {
private afterInitialLoad: Array<(settings: Meteor.Settings) => void> = [];

Expand Down Expand Up @@ -183,7 +192,7 @@ class Settings extends SettingsBase {
};
}

const existentSetting = SettingsModel.db.findOne(query);
const existentSetting = SettingsModel.findOne(query);
if (existentSetting) {
if (existentSetting.editor || !updateOperations.$setOnInsert.editor) {
return true;
Expand All @@ -204,10 +213,10 @@ class Settings extends SettingsBase {
/*
* Add a setting group
*/
addGroup(_id: string, cb: () => void): boolean;
addGroup(_id: string, cb: addGroupCallback): boolean;

// eslint-disable-next-line no-dupe-class-members
addGroup(_id: string, options: ISettingAddGroupOptions | (() => void) = {}, cb?: () => void): boolean {
addGroup(_id: string, options: ISettingAddGroupOptions | addGroupCallback = {}, cb?: addGroupCallback): boolean {
if (!_id) {
return false;
}
Expand Down Expand Up @@ -257,7 +266,7 @@ class Settings extends SettingsBase {
options.group = _id;
return this.add(id, value, options);
},
section: (section: string, cb: () => void) => cb.call({
section: (section: string, cb: addSectionCallback) => cb.call({
add: (id: string, value: SettingValue, options: ISettingAddOptions = {}) => {
options.group = _id;
options.section = section;
Expand Down
4 changes: 4 additions & 0 deletions mocha.opts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
--require ts-node/register
--require babel-mocha-es6-compiler
--require babel-polyfill
--reporter spec
--ui bdd
--watch-extensions ts
--extension ts
app/**/*.tests.js app/**/*.tests.ts
Loading

0 comments on commit 2f73e1b

Please sign in to comment.