Skip to content

Commit

Permalink
Enhance RTDB v2 Triggers (#1153)
Browse files Browse the repository at this point in the history
* moving change into v2 core

* moving Change to common and fixing imports

* fix tsdoc

* rexporting Change from v1&v2

* remove unused functions from Change namespace

* fixing argument

* rename interface to onValue* and rework Change

Co-authored-by: Thomas Bouldin <inlined@users.noreply.github.com>
  • Loading branch information
colerogers and inlined committed Jun 28, 2022
1 parent 96f9e86 commit 139e1d9
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 213 deletions.
120 changes: 120 additions & 0 deletions spec/common/change.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import { expect } from 'chai';
import * as change from '../../src/common/change';

describe('Change', () => {
describe('applyFieldMask', () => {
const after = {
foo: 'bar',
num: 2,
obj: {
a: 1,
b: 2,
},
};

it('should handle deleted values', () => {
const sparseBefore = { baz: 'qux' };
const fieldMask = 'baz';
expect(
change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
num: 2,
obj: {
a: 1,
b: 2,
},
baz: 'qux',
});
});

it('should handle created values', () => {
const sparseBefore = {};
const fieldMask = 'num,obj.a';
expect(
change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
obj: {
b: 2,
},
});
});

it('should handle mutated values', () => {
const sparseBefore = {
num: 3,
obj: {
a: 3,
},
};
const fieldMask = 'num,obj.a';
expect(
change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
num: 3,
obj: {
a: 3,
b: 2,
},
});
});
});

describe('fromJSON', () => {
it('should create a Change object with a `before` and `after`', () => {
const created = change.Change.fromJSON<any>({
before: { foo: 'bar' },
after: { foo: 'faz' },
});
expect(created instanceof change.Change).to.equal(true);
expect(created.before).to.deep.equal({ foo: 'bar' });
expect(created.after).to.deep.equal({ foo: 'faz' });
});

it('should apply the customizer function to `before` and `after`', () => {
function customizer<T>(input: any) {
input.another = 'value';
return input as T;
}
const created = change.Change.fromJSON<object>(
{
before: { foo: 'bar' },
after: { foo: 'faz' },
},
customizer
);
expect(created.before).to.deep.equal({
foo: 'bar',
another: 'value',
});
expect(created.after).to.deep.equal({
foo: 'faz',
another: 'value',
});
});
});
});
97 changes: 0 additions & 97 deletions spec/v1/cloud-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { expect } from 'chai';
import * as _ from 'lodash';

import {
Change,
Event,
EventContext,
makeCloudFunction,
Expand Down Expand Up @@ -354,99 +353,3 @@ describe('makeAuth and makeAuthType', () => {
});
});
});

describe('Change', () => {
describe('applyFieldMask', () => {
const after = {
foo: 'bar',
num: 2,
obj: {
a: 1,
b: 2,
},
};

it('should handle deleted values', () => {
const sparseBefore = { baz: 'qux' };
const fieldMask = 'baz';
expect(
Change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
num: 2,
obj: {
a: 1,
b: 2,
},
baz: 'qux',
});
});

it('should handle created values', () => {
const sparseBefore = {};
const fieldMask = 'num,obj.a';
expect(
Change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
obj: {
b: 2,
},
});
});

it('should handle mutated values', () => {
const sparseBefore = {
num: 3,
obj: {
a: 3,
},
};
const fieldMask = 'num,obj.a';
expect(
Change.applyFieldMask(sparseBefore, after, fieldMask)
).to.deep.equal({
foo: 'bar',
num: 3,
obj: {
a: 3,
b: 2,
},
});
});
});

describe('fromJSON', () => {
it('should create a Change object with a `before` and `after`', () => {
const created = Change.fromJSON<any>({
before: { foo: 'bar' },
after: { foo: 'faz' },
});
expect(created instanceof Change).to.equal(true);
expect(created.before).to.deep.equal({ foo: 'bar' });
expect(created.after).to.deep.equal({ foo: 'faz' });
});

it('should apply the customizer function to `before` and `after`', () => {
function customizer<T>(input: any) {
_.set(input, 'another', 'value');
return input as T;
}
const created = Change.fromJSON<object>(
{
before: { foo: 'bar' },
after: { foo: 'faz' },
},
customizer
);
expect(created.before).to.deep.equal({
foo: 'bar',
another: 'value',
});
expect(created.after).to.deep.equal({
foo: 'faz',
another: 'value',
});
});
});
});
24 changes: 12 additions & 12 deletions spec/v2/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,9 @@ describe('database', () => {
});
});

describe('onRefWritten', () => {
describe('onValueWritten', () => {
it('should create a function with a reference', () => {
const func = database.onRefWritten('/foo/{bar}/', (event) => 2);
const func = database.onValueWritten('/foo/{bar}/', (event) => 2);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
Expand All @@ -383,7 +383,7 @@ describe('database', () => {
});

it('should create a function with opts', () => {
const func = database.onRefWritten(
const func = database.onValueWritten(
{
ref: '/foo/{path=**}/{bar}/',
instance: 'my-instance',
Expand Down Expand Up @@ -414,9 +414,9 @@ describe('database', () => {
});
});

describe('onRefCreated', () => {
describe('onValueCreated', () => {
it('should create a function with a reference', () => {
const func = database.onRefCreated('/foo/{bar}/', (event) => 2);
const func = database.onValueCreated('/foo/{bar}/', (event) => 2);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
Expand All @@ -434,7 +434,7 @@ describe('database', () => {
});

it('should create a function with opts', () => {
const func = database.onRefCreated(
const func = database.onValueCreated(
{
ref: '/foo/{path=**}/{bar}/',
instance: 'my-instance',
Expand Down Expand Up @@ -465,9 +465,9 @@ describe('database', () => {
});
});

describe('onRefUpdated', () => {
describe('onValueUpdated', () => {
it('should create a function with a reference', () => {
const func = database.onRefUpdated('/foo/{bar}/', (event) => 2);
const func = database.onValueUpdated('/foo/{bar}/', (event) => 2);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
Expand All @@ -485,7 +485,7 @@ describe('database', () => {
});

it('should create a function with opts', () => {
const func = database.onRefUpdated(
const func = database.onValueUpdated(
{
ref: '/foo/{path=**}/{bar}/',
instance: 'my-instance',
Expand Down Expand Up @@ -516,9 +516,9 @@ describe('database', () => {
});
});

describe('onRefDeleted', () => {
describe('onValueDeleted', () => {
it('should create a function with a reference', () => {
const func = database.onRefDeleted('/foo/{bar}/', (event) => 2);
const func = database.onValueDeleted('/foo/{bar}/', (event) => 2);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
Expand All @@ -536,7 +536,7 @@ describe('database', () => {
});

it('should create a function with opts', () => {
const func = database.onRefDeleted(
const func = database.onValueDeleted(
{
ref: '/foo/{path=**}/{bar}/',
instance: 'my-instance',
Expand Down

0 comments on commit 139e1d9

Please sign in to comment.