Skip to content

Commit c179f30

Browse files
test(ReplicateAttributes): Add tests for deleting field in update
1 parent a8bc2f8 commit c179f30

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

test/functions/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ module.exports.replicateMasterToDetail = integrify({
3939
},
4040
});
4141

42+
module.exports.replicateMasterDeleteWhenEmpty = integrify({
43+
rule: 'REPLICATE_ATTRIBUTES',
44+
source: {
45+
collection: 'master',
46+
},
47+
targets: [
48+
{
49+
collection: 'detail1',
50+
foreignKey: 'tempId',
51+
attributeMapping: {
52+
masterDetail1: 'foreignDetail1',
53+
masterDetail2: 'foreignDetail2',
54+
},
55+
deleteMissing: true,
56+
},
57+
],
58+
hooks: {
59+
pre: (change, context) => {
60+
setState({
61+
change,
62+
context,
63+
});
64+
},
65+
},
66+
});
67+
4268
module.exports.deleteReferencesToMaster = integrify({
4369
rule: 'DELETE_REFERENCES',
4470
source: {

test/functions/integrify.rules.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ module.exports = [
2727
},
2828
],
2929
},
30+
{
31+
rule: 'REPLICATE_ATTRIBUTES',
32+
name: 'replicateMasterDeleteWhenEmpty',
33+
source: {
34+
collection: 'master',
35+
},
36+
targets: [
37+
{
38+
collection: 'detail1',
39+
foreignKey: 'tempId',
40+
attributeMapping: {
41+
masterDetail1: 'foreignDetail1',
42+
masterDetail2: 'foreignDetail2',
43+
},
44+
deleteMissing: true,
45+
},
46+
],
47+
},
3048
{
3149
rule: 'DELETE_REFERENCES',
3250
name: 'deleteReferencesToMaster',

test/unit.test.js

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ testsuites.forEach(testsuite => {
6161
testDeleteReferences(sut, t, name));
6262
test(`[${name}] test maintain count`, async t => testMaintainCount(sut, t));
6363

64+
test(`[${name}] test replicate attributes delete when field is not there`, async t =>
65+
testReplicateAttributesDeleteEmpty(sut, t, name));
66+
6467
test(`[${name}] test delete with masterId in target reference`, async t =>
6568
testDeleteParamReferences(sut, t, name));
6669
test(`[${name}] test delete with snapshot fields in target reference`, async t =>
@@ -163,7 +166,9 @@ async function testReplicateAttributes(sut, t, name) {
163166

164167
// Call trigger to replicate attributes from master
165168
const beforeSnap = fft.firestore.makeDocumentSnapshot(
166-
{},
169+
{
170+
masterField5: 'missing',
171+
},
167172
`master/${masterId}`
168173
);
169174
const afterSnap = fft.firestore.makeDocumentSnapshot(
@@ -226,6 +231,68 @@ async function testReplicateAttributes(sut, t, name) {
226231
await t.pass();
227232
}
228233

234+
async function testReplicateAttributesDeleteEmpty(sut, t, name) {
235+
// Add a couple of detail documents to follow master
236+
const masterId = makeid();
237+
await db.collection('detail1').add({
238+
tempId: masterId,
239+
foreignDetail1: 'foreign_detail_1',
240+
foreignDetail2: 'foreign_detail_2',
241+
});
242+
243+
// Call trigger to replicate attributes from master
244+
const beforeSnap = fft.firestore.makeDocumentSnapshot(
245+
{
246+
masterDetail1: 'after1',
247+
masterDetail2: 'after2',
248+
},
249+
`master/${masterId}`
250+
);
251+
const afterSnap = fft.firestore.makeDocumentSnapshot(
252+
{
253+
masterDetail2: 'after3',
254+
},
255+
`master/${masterId}`
256+
);
257+
const change = fft.makeChange(beforeSnap, afterSnap);
258+
const wrapped = fft.wrap(sut.replicateMasterDeleteWhenEmpty);
259+
setState({
260+
change: null,
261+
context: null,
262+
});
263+
await wrapped(change, {
264+
params: {
265+
masterId: masterId,
266+
},
267+
});
268+
269+
// Assert pre-hook was called (only for rules-in-situ)
270+
if (name === 'rules-in-situ') {
271+
const state = getState();
272+
t.truthy(state.change);
273+
t.truthy(state.context);
274+
t.is(state.context.params.masterId, masterId);
275+
}
276+
277+
// Assert that attributes get replicated to detail documents
278+
await assertQuerySizeEventually(
279+
db
280+
.collection('detail1')
281+
.where('tempId', '==', masterId)
282+
.where('foreignDetail1', '==', 'foreign_detail_1'),
283+
0
284+
);
285+
await assertQuerySizeEventually(
286+
db
287+
.collection('detail1')
288+
.where('tempId', '==', masterId)
289+
.where('foreignDetail2', '==', 'after3'),
290+
1
291+
);
292+
293+
await t.pass();
294+
}
295+
229296
async function testDeleteReferences(sut, t, name) {
230297
// Create some docs referencing master doc
231298
const masterId = makeid();

0 commit comments

Comments
 (0)