Skip to content

Commit

Permalink
test(error): Add tests for error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
anishkny committed Nov 28, 2020
1 parent 2e74037 commit 7db96f3
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
3 changes: 3 additions & 0 deletions test/functions-absent-rules-file/index.js
@@ -0,0 +1,3 @@
const { integrify } = require('../../lib');

module.exports = integrify(); // Rules will be loaded from "integrify.rules.js"
3 changes: 3 additions & 0 deletions test/functions-bad-rules-file/index.js
@@ -0,0 +1,3 @@
const { integrify } = require('../../lib');

module.exports = integrify(); // Rules will be loaded from "integrify.rules.js"
5 changes: 5 additions & 0 deletions test/functions-bad-rules-file/integrify.rules.js
@@ -0,0 +1,5 @@
module.exports = [
{
rule: 'BAD_RULE_3038d3c1644b',
},
];
6 changes: 5 additions & 1 deletion test/functions/index.js
Expand Up @@ -3,11 +3,12 @@ const { setState } = require('./stateMachine');

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp({ projectId: 'dummy-project' });
admin.initializeApp();
const db = admin.firestore();

integrify({ config: { db, functions } });

// Specify rules in situ
module.exports.replicateMasterToDetail = integrify({
rule: 'REPLICATE_ATTRIBUTES',
source: {
Expand Down Expand Up @@ -73,3 +74,6 @@ module.exports.maintainFavoritesCount = integrify({
attribute: 'favoritesCount',
},
});

// Specify rules from "./integrify.rules.js"
const rulesFromFiles = integrify();
58 changes: 58 additions & 0 deletions test/functions/integrify.rules.js
@@ -0,0 +1,58 @@
module.exports = [
{
rule: 'REPLICATE_ATTRIBUTES',
name: 'replicateMasterToDetailFromFile',
source: {
collection: 'master',
},
targets: [
{
collection: 'detail1',
foreignKey: 'masterId',
attributeMapping: {
masterField1: 'detail1Field1',
masterField2: 'detail1Field2',
},
},
{
collection: 'detail2',
foreignKey: 'masterId',
attributeMapping: {
masterField1: 'detail2Field1',
masterField3: 'detail2Field3',
},
isCollectionGroup: true,
},
],
},
{
rule: 'DELETE_REFERENCES',
name: 'deleteReferencesToMasterFromFile',
source: {
collection: 'master',
},
targets: [
{
collection: 'detail1',
foreignKey: 'masterId',
},
{
collection: 'detail2',
foreignKey: 'masterId',
isCollectionGroup: true,
},
],
},
{
rule: 'MAINTAIN_COUNT',
name: 'maintainFavoritesCountFromFile',
source: {
collection: 'favorites',
foreignKey: 'articleId',
},
target: {
collection: 'articles',
attribute: 'favoritesCount',
},
},
];
21 changes: 19 additions & 2 deletions test/main.test.js
Expand Up @@ -2,6 +2,23 @@ const assert = require('chai').assert;

const { integrify } = require('../lib');

it('TODO', function () {
assert.equal(1, 1);
describe('Error conditions', () => {
it('should error on bad rule', function () {
assert.throws(
() => integrify({ rule: 'BAD_RULE_ea8e3a2a2d3e' }),
/Unknown rule/i
);
assert.throws(() => require('./functions-bad-rules-file'), /Unknown rule/i);
});

it('should error on no rule or config', function () {
assert.throws(() => integrify(42), /Input must be rule or config/i);
});

it('should error on absent config file', function () {
assert.throws(
() => require('./functions-absent-rules-file'),
/Rules file not found/i
);
});
});
1 change: 1 addition & 0 deletions test/run-tests.sh
Expand Up @@ -7,6 +7,7 @@ cd $SCRIPTDIR
pwd
rm -rf ../.nyc_output ../coverage

export GCLOUD_PROJECT=dummy-project
export FIRESTORE_EMULATOR_HOST='localhost:8080'
npx nyc -r html -r text -r lcov firebase emulators:exec \
'mocha --bail --exit --jobs 1 --timeout 30s *.test.js'
Expand Down

0 comments on commit 7db96f3

Please sign in to comment.