如標題中所述,我不知道為什么Database.collection(...).onSnapshot不將函式方法作為函式拾取。
我收到的訊息錯誤是:TypeError: Database.collection(...).onSnapshot is not a function
我試圖讓它像這樣作業:
test.only('should react when a new record is created', async () => {
jest.mock('@google-cloud/firestore', () => jest.fn(() => ({
collection: jest.fn()
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockImplementationOnce(function () { return this })
.mockResolvedValueOnce(() => {
return {
onSnapshot: () => {} // HOW TO MAKE IT WORK AS A FUNCTION?
}
}),
get: jest.fn().mockResolvedValue({ docs: [] }),
doc: jest.fn().mockReturnThis(),
set: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
select: jest.fn().mockReturnThis()
})))
const StatisticsService = require('./index')
await StatisticsService()
})
代碼邏輯:
const Firestore = require('@google-cloud/firestore')
const Database = new Firestore()
const collections = ['products', 'users', 'orders']
collections.forEach(collection => {
Database.collection(collection).onSnapshot(snapshot => {
console.log('??????', snapshot)
snapshot.docChanges().forEach(change => {
if (change.type === 'added') this.onDatabaseRecordCreated(collection, change.doc.data())
if (change.type === 'modified') this.onDatabaseRecordUpdated(collection, change.doc.data())
if (change.type === 'removed') this.onDatabaseRecordRemoved(collection, change.doc.data())
})
}, error => {
console.error(error)
})
})
uj5u.com熱心網友回復:
嘗試這個:
index.js:
const Firestore = require('@google-cloud/firestore');
const Database = new Firestore();
async function StatisticsService() {
const collections = ['products', 'users', 'orders'];
collections.forEach((collection) => {
Database.collection(collection).onSnapshot(
(snapshot) => {
// console.log('??????', snapshot);
snapshot.docChanges().forEach((change) => {
console.log(change);
});
},
(error) => {
console.error(error);
}
);
});
}
module.exports = StatisticsService;
index.test.js:
describe('71115363', () => {
test('should pass', async () => {
jest.mock('@google-cloud/firestore', () => {
const mChanges = [{ name: 'fake data' }];
const mSnapshot = {
docChanges: jest.fn().mockReturnValue(mChanges),
};
return jest.fn(() => ({
collection: jest.fn().mockReturnThis(),
onSnapshot: jest.fn().mockImplementation((callback) => {
callback(mSnapshot);
}),
}));
});
const StatisticsService = require('./index');
await StatisticsService();
});
});
測驗結果:
PASS stackoverflow/71115363/index.test.js
71115363
? should pass (103 ms)
console.log
{ name: 'fake data' }
at stackoverflow/71115363/index.js:8:17
at Array.forEach (<anonymous>)
console.log
{ name: 'fake data' }
at stackoverflow/71115363/index.js:8:17
at Array.forEach (<anonymous>)
console.log
{ name: 'fake data' }
at stackoverflow/71115363/index.js:8:17
at Array.forEach (<anonymous>)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.184 s, estimated 2 s
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427126.html
