Skip to content

Commit

Permalink
Merge pull request #22 from GitLiveApp/refactor-and-add-github-actions
Browse files Browse the repository at this point in the history
Refactor, add GitHub actions and covert to esm module
  • Loading branch information
nbransby authored Oct 21, 2022
2 parents ce102cf + 94ae571 commit 71d36f8
Show file tree
Hide file tree
Showing 26 changed files with 1,337 additions and 155 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Publish to GitHub Packages

on:
release:
types: [released]

jobs:
publish:
name: Publish to GitHub packages
runs-on: ubuntu-latest

steps:
- name: Checkout source
uses: actions/checkout@v3

- name: Restore cache
uses: actions/cache@v3
with:
path: |
~/.npm
key: |
${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: Install Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://npm.pkg.github.com/
scope: "@GitLiveApp"

- name: Install dependencies
run: npm install

- name: Build
run: npm run build

- name: Create .npmrc file
run: echo "registry=https://npm.pkg.github.com/@GitLiveApp" >> .npmrc

- name: Publish to Github packages
run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
54 changes: 54 additions & 0 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Run Tests CI

on:
pull_request:
branches: [ master ]

jobs:
build:
name: Build
runs-on: ubuntu-latest

steps:
- name: Checkout source
uses: actions/checkout@v3

- name: Restore cache
uses: actions/cache@v3
with:
path: |
~/.npm
key: |
${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: Install Node.js 16
uses: actions/setup-node@v3
with:
node-version: 16

- name: Install dependencies
run: npm install

- name: Install Java 15
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '15'
java-package: jdk
architecture: x64

- name: Run integration tests against Firebase emulator
run: |
npm install -g firebase-tools
firebase emulators:exec --only firestore --project integrify-emulator 'npm run test:ci'
- name: Generate Jest test report
uses: IgnusG/jest-report-action@v2.3.3
if: always()
with:
access-token: ${{ secrets.GITHUB_TOKEN }}
junit-file: 'junit.xml'
run-name: 'Jest Test Report'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ coverage
.vscode
*.log
*.xml
.DS_Store
17 changes: 17 additions & 0 deletions __mocks__/firebase-admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// this module will be automatically mocked no need to call it in your test files
import admin from 'firebase-admin';

process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8080';
process.env.GOOGLE_CLOUD_PROJECT = 'integrify-emulator';
process.env.PUBSUB_PROJECT_ID = 'integrify-emulator';

// initializeApp takes the environment variables into account
admin.initializeApp();

const firestore = admin.firestore;

module.exports = {
...admin,
firestore,
initializeApp: jest.fn()
}
93 changes: 93 additions & 0 deletions __test__/common.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { getPrimaryKey, replaceReferencesWith } from "../src/common";

describe("common - getPrimaryKey", () => {

it('should return a single primary key', async () => {
let targetCollection = 'collection/{collectionId}';
let result = getPrimaryKey(targetCollection);

expect(result.hasPrimaryKey).toBeTruthy();
expect(result.primaryKey).toBe('collectionId');
});

it('should return the last key if there is more than one', async () => {
let targetCollection = 'collection/{collectionId}/some_detail/{detailId}';
let result = getPrimaryKey(targetCollection);

expect(result.hasPrimaryKey).toBeTruthy();
expect(result.primaryKey).toBe('detailId');
});

it('should return the default masterId for missing primary key', async () => {
let targetCollection = 'collection';
let result = getPrimaryKey(targetCollection);

expect(result.hasPrimaryKey).toBeFalsy();
expect(result.primaryKey).toBe('masterId');
});

});

describe("common - replaceReferencesWith", () => {

it('should return false for hasFields and unchanged targetCollection', async () => {
let collectionId = makeId();
let targetCollection = 'collection';
let documentData = {
collectionId,
};

let result = replaceReferencesWith(documentData, targetCollection);

expect(result.hasFields).toBeFalsy();
expect(result.targetCollection).toBe('collection');
});

it('should return true for hasFields and the collectionId in the targetCollection', async () => {
let collectionId = makeId();
let targetCollection = 'collection/$collectionId/some_detail';
let documentData = {
collectionId,
};

let result = replaceReferencesWith(documentData, targetCollection);

expect(result.hasFields).toBeTruthy();
expect(result.targetCollection).toBe(`collection/${collectionId}/some_detail`);
});

it('should return true for hasFields and the testId and userId in the targetCollection', async () => {
let collectionId = makeId();
let testId = makeId();
let userId = makeId();
let targetCollection = 'collection/$testId/some_detail/$userId';
let documentData = {
collectionId,
testId,
userId,
};

let result = replaceReferencesWith(documentData, targetCollection);

expect(result.hasFields).toBeTruthy();
expect(result.targetCollection).toBe(`collection/${testId}/some_detail/${userId}`);
});

it('should throw an error if the targetCollection uses a field that does not exist', async () => {
let targetCollection = 'collection/$collectionId/some_detail';
let documentData = {};

const error = () => {
replaceReferencesWith(documentData, targetCollection);
};

expect(error).toThrow('integrify: Missing dynamic reference: [$collectionId]');
});

const makeId = () => {
return Math.random()
.toString(36)
.substr(2);
}

});
145 changes: 145 additions & 0 deletions __test__/files/deleteFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { integrify } from '../../src/index';
import { firestore } from 'firebase-admin';
import * as functions from 'firebase-functions';

integrify({ config: { db: firestore(), functions, verbose: true } });

export const deleteReferencesInCollectionUsingDefault = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master',
},
targets: [
{
collection: 'detail1',
foreignKey: 'someKey',
}
]
});

export const deleteReferencesInCollection = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master/{masterId}',
},
targets: [
{
collection: 'detail1',
foreignKey: 'masterId',
}
]
});

export const deleteReferencesInCollectionGroup = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master/{masterId}',
},
targets: [
{
collection: 'detail2',
foreignKey: 'masterId',
isCollectionGroup: true,
}
]
});

export const deleteReferencesWithRenamedSourceParam = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master/{primaryKey}',
},
targets: [
{
collection: 'detail1',
foreignKey: 'primaryKey',
}
],
});

export const deleteReferencesWithSnapshotFieldsInSource = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master/{anotherKey}',
},
targets: [
{
collection: 'detail1',
foreignKey: 'anotherKey',
},
],
});

export const deleteAllReferencesInCollectionU = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master',
},
targets: [
{
collection: 'detail1',
deleteAll: true,
}
]
});

export const deleteMissingPrimaryKey = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master/{noKey}',
},
targets: [
{
collection: 'detail1',
foreignKey: 'someKey',
}
]
});

export const deleteMissingForeignKeyAndDeleteAll = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master/{anotherKey}',
},
targets: [
{
collection: 'detail1',
}
]
});

export const deleteReferencesInCollectionWithPreHook = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master/{masterId}',
},
targets: [
{
collection: 'detail1',
foreignKey: 'masterId',
}
],
hooks: {
pre: (snap, context) => {
firestore().collection('pre').doc('test').set({ pre: true });
},
}
});

export const deleteReferencesInCollectionWithPostHook = integrify({
rule: 'DELETE_REFERENCES',
source: {
collection: 'master/{masterId}',
},
targets: [
{
collection: 'detail1',
foreignKey: 'masterId',
}
],
hooks: {
post: (snap, context) => {
firestore().collection('post').doc('test').set({ post: true });
},
}
});
Loading

0 comments on commit 71d36f8

Please sign in to comment.