Skip to content

Commit

Permalink
Add jest matcher
Browse files Browse the repository at this point in the history
Adding jest matchers to conveniently use targaryen with jest.

Usage:
```
const { getDatabase, toAllowRead, toAllowUpdate, toAllowWrite, json } = require('targaryen/plugins/jest');

expect.extend({
  toAllowRead,
  toAllowUpdate,
  toAllowWrite,
});

const RULES_PATH = 'database.rules.json';
const rules = json.loadSync(RULES_PATH);
const initialData = {};

const database = getDatabase(rules, initialData);

test('basic', () => {
  expect(database.as(null)).not.toAllowRead('/user');
  expect(database.as(null)).toAllowRead('/public');
  expect(database.as({ uid: '1234'})).toAllowWrite('/user/1234', {
    name: 'Anna',
  });
});

```

or using the generic matcher:
```
const { getDebugDatabase, toBeAllowed, json } = require('targaryen/plugins/jest');

expect.extend({
  toBeAllowed,
});

const RULES_PATH = 'database.rules.json';
const rules = json.loadSync(RULES_PATH);
const initialData = {};

// NOTE: Create a database with debug set to true for detailed errors
const database = getDebugDatabase(rules, initialData);

test('generic', () => {
  expect(database.as(null).read('/user')).not.toBeAllowed();
  expect(database.as({ uid: '1234' }).write('/user/1234', {
    name: 'Anna',
  })).toBeAllowed();
});

```
  • Loading branch information
a-xin committed May 9, 2018
1 parent f3281a0 commit 4f3fc3b
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions plugins/jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* targaryen/plugins/jest - Reference implementation of a jest plugin for
* targaryen.
*
*/

'use strict';

const targaryen = require('../');

function toBeAllowed({ info, allowed }) {
const pass = allowed === true;
const message = pass
? () => `Expected operation to be ${this.utils.EXPECTED_COLOR('denied')} but it was ${this.utils.RECEIVED_COLOR('allowed')}\n\n${info}`
: () => `Expected operation to be ${this.utils.EXPECTED_COLOR('allowed')} but it was ${this.utils.RECEIVED_COLOR('denied')}\n\n${info}`;

return {
message,
pass,
};
}

function toAllowRead(database, path, options) {
const { info, allowed } = database
.with({ debug: true })
.read(path, options);

const pass = allowed === true;
const message = pass
? () => `Expected ${this.utils.EXPECTED_COLOR('read')} to be ${this.utils.EXPECTED_COLOR('denied')} but it was ${this.utils.RECEIVED_COLOR('allowed')}\n\n${info}`
: () => `Expected ${this.utils.EXPECTED_COLOR('read')} to be ${this.utils.EXPECTED_COLOR('allowed')} but it was ${this.utils.RECEIVED_COLOR('denied')}\n\n${info}`;

return {
message,
pass,
};
}

function toAllowWrite(database, path, value, options) {
const { info, allowed } = database
.with({ debug: true })
.write(path, value, options);

const pass = allowed === true;
const message = pass
? () => `Expected ${this.utils.EXPECTED_COLOR('write')} to be ${this.utils.EXPECTED_COLOR('denied')} but it was ${this.utils.RECEIVED_COLOR('allowed')}\n\n${info}`
: () => `Expected ${this.utils.EXPECTED_COLOR('write')} to be ${this.utils.EXPECTED_COLOR('allowed')} but it was ${this.utils.RECEIVED_COLOR('denied')}\n\n${info}`;

return {
message,
pass,
};
}

function toAllowUpdate(database, path, patch, options) {
const { info, allowed } = database
.with({ debug: true })
.update(path, patch, options);

const pass = allowed === true;
const message = pass
? () => `Expected ${this.utils.EXPECTED_COLOR('update')} to be ${this.utils.EXPECTED_COLOR('denied')} but it was ${this.utils.RECEIVED_COLOR('allowed')}\n\n${info}`
: () => `Expected ${this.utils.EXPECTED_COLOR('update')} to be ${this.utils.EXPECTED_COLOR('allowed')} but it was ${this.utils.RECEIVED_COLOR('denied')}\n\n${info}`;

return {
message,
pass,
};
}

/**
* Simple wrapper for `targaryen.database()` for conveniently creating a
* database for a jest test.
*/
function getDatabase(...args) {
return targaryen.database(...args);
}

/**
* Simple wrapper for `targaryen.database()` that also enables debug mode for
* detailed error messages.
*/
function getDebugDatabase(...args) {
return targaryen.database(...args).with({ debug: true });
}

const jestTargaryen = {
toBeAllowed,
toAllowRead,
toAllowWrite,
toAllowUpdate,

// NOTE: Exported for convenience only
getDatabase,
getDebugDatabase,
json: require('firebase-json'),
users: targaryen.util.users,
};

module.exports = jestTargaryen;

0 comments on commit 4f3fc3b

Please sign in to comment.