mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-16 17:51:43 +10:00
Firebase Stub: added more unit tests
This commit is contained in:
@ -95,22 +95,6 @@ describe('FirebaseStub', () => {
|
|||||||
expect(timestamp).toBeGreaterThanOrEqual(now);
|
expect(timestamp).toBeGreaterThanOrEqual(now);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("can spy on Reference 'update' function", async () => {
|
|
||||||
const referencePath = `${DatabaseConstants.resumesPath}/123456`;
|
|
||||||
const functionSpy = jest.spyOn(
|
|
||||||
FirebaseStub.database().ref(referencePath),
|
|
||||||
'update',
|
|
||||||
);
|
|
||||||
const updateArgument = 'test value 123';
|
|
||||||
|
|
||||||
await FirebaseStub.database().ref(referencePath).update(updateArgument);
|
|
||||||
|
|
||||||
expect(functionSpy).toHaveBeenCalledTimes(1);
|
|
||||||
const functionCallArgument = functionSpy.mock.calls[0][0];
|
|
||||||
expect(functionCallArgument).toBeTruthy();
|
|
||||||
expect(functionCallArgument).toEqual(updateArgument);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('initializing data sets up resumes and users', async () => {
|
it('initializing data sets up resumes and users', async () => {
|
||||||
const resumesRef = FirebaseStub.database().ref(
|
const resumesRef = FirebaseStub.database().ref(
|
||||||
DatabaseConstants.resumesPath,
|
DatabaseConstants.resumesPath,
|
||||||
@ -196,7 +180,9 @@ describe('FirebaseStub', () => {
|
|||||||
expect(user).toBeNull();
|
expect(user).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("triggers 'value' event with true when listening for data changes on the connected reference path", async () => {
|
describe('on function', () => {
|
||||||
|
describe('value event', () => {
|
||||||
|
it('triggers event with true if on the connected reference path', async () => {
|
||||||
let snapshotValue = null;
|
let snapshotValue = null;
|
||||||
|
|
||||||
FirebaseStub.database()
|
FirebaseStub.database()
|
||||||
@ -213,7 +199,7 @@ describe('FirebaseStub', () => {
|
|||||||
expect(snapshotValue).toBe(true);
|
expect(snapshotValue).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("triggers 'value' event with resumes when listening for data changes on the resumes reference path", async () => {
|
it('triggers event with resumes if on the resumes reference path', async () => {
|
||||||
const resumesDataSnapshot = await FirebaseStub.database()
|
const resumesDataSnapshot = await FirebaseStub.database()
|
||||||
.ref(DatabaseConstants.resumesPath)
|
.ref(DatabaseConstants.resumesPath)
|
||||||
.once('value');
|
.once('value');
|
||||||
@ -233,6 +219,8 @@ describe('FirebaseStub', () => {
|
|||||||
expect(snapshotValue).not.toBeNull();
|
expect(snapshotValue).not.toBeNull();
|
||||||
expect(snapshotValue).toEqual(resumes);
|
expect(snapshotValue).toEqual(resumes);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('can filter resumes by user', async () => {
|
it('can filter resumes by user', async () => {
|
||||||
let snapshotValue = null;
|
let snapshotValue = null;
|
||||||
@ -281,29 +269,45 @@ describe('FirebaseStub', () => {
|
|||||||
expect(reference.equalToValue).toHaveLength(0);
|
expect(reference.equalToValue).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("triggers 'value' event with resumes when creating a new resume", async () => {
|
describe('set function', () => {
|
||||||
const userUid = DatabaseConstants.user1.uid;
|
it('inserts data', async () => {
|
||||||
|
const existingResume = (
|
||||||
|
await FirebaseStub.database()
|
||||||
|
.ref(
|
||||||
|
`${DatabaseConstants.resumesPath}/${DatabaseConstants.demoStateResume1Id}`,
|
||||||
|
)
|
||||||
|
.once('value')
|
||||||
|
).val();
|
||||||
|
expect(existingResume).toBeTruthy();
|
||||||
|
|
||||||
|
const newResume = JSON.parse(JSON.stringify(existingResume));
|
||||||
|
newResume.id = 'newre1';
|
||||||
|
newResume.name = `Test Resume ${newResume.id}`;
|
||||||
|
await FirebaseStub.database()
|
||||||
|
.ref(`${DatabaseConstants.resumesPath}/${newResume.id}`)
|
||||||
|
.set(newResume);
|
||||||
|
|
||||||
|
const actualResume = (
|
||||||
|
await FirebaseStub.database()
|
||||||
|
.ref(`${DatabaseConstants.resumesPath}/${newResume.id}`)
|
||||||
|
.once('value')
|
||||||
|
).val();
|
||||||
|
expect(actualResume).toBeTruthy();
|
||||||
|
expect(actualResume).toEqual(newResume);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('triggers events', async () => {
|
||||||
let snapshotValue = null;
|
let snapshotValue = null;
|
||||||
const callback = jest.fn((snapshot) => {
|
const callback = jest.fn((snapshot) => {
|
||||||
snapshotValue = snapshot.val();
|
snapshotValue = snapshot.val();
|
||||||
});
|
});
|
||||||
|
|
||||||
FirebaseStub.database()
|
FirebaseStub.database()
|
||||||
.ref(DatabaseConstants.resumesPath)
|
.ref(DatabaseConstants.resumesPath)
|
||||||
.orderByChild('user')
|
.orderByChild('user')
|
||||||
.equalTo(userUid)
|
.equalTo(DatabaseConstants.user1.uid)
|
||||||
.on('value', callback);
|
.on('value', callback);
|
||||||
|
|
||||||
await waitFor(() => callback.mock.calls[0][0]);
|
await waitFor(() => callback.mock.calls[0][0]);
|
||||||
|
|
||||||
expect(callback.mock.calls).toHaveLength(1);
|
|
||||||
callback.mockClear();
|
callback.mockClear();
|
||||||
expect(snapshotValue).not.toBeNull();
|
|
||||||
expect(Object.keys(snapshotValue)).toHaveLength(2);
|
|
||||||
Object.values(snapshotValue).forEach((resume) =>
|
|
||||||
expect(resume.user).toEqual(userUid),
|
|
||||||
);
|
|
||||||
snapshotValue = null;
|
snapshotValue = null;
|
||||||
|
|
||||||
const existingResume = (
|
const existingResume = (
|
||||||
@ -314,12 +318,10 @@ describe('FirebaseStub', () => {
|
|||||||
.once('value')
|
.once('value')
|
||||||
).val();
|
).val();
|
||||||
expect(existingResume).toBeTruthy();
|
expect(existingResume).toBeTruthy();
|
||||||
expect(existingResume.user).toEqual(userUid);
|
|
||||||
|
|
||||||
const newResume = JSON.parse(JSON.stringify(existingResume));
|
const newResume = JSON.parse(JSON.stringify(existingResume));
|
||||||
newResume.id = 'newre1';
|
newResume.id = 'newre1';
|
||||||
newResume.name = `Test Resume ${newResume.id}`;
|
newResume.name = `Test Resume ${newResume.id}`;
|
||||||
|
|
||||||
await FirebaseStub.database()
|
await FirebaseStub.database()
|
||||||
.ref(`${DatabaseConstants.resumesPath}/${newResume.id}`)
|
.ref(`${DatabaseConstants.resumesPath}/${newResume.id}`)
|
||||||
.set(newResume);
|
.set(newResume);
|
||||||
@ -330,32 +332,64 @@ describe('FirebaseStub', () => {
|
|||||||
expect(snapshotValue).not.toBeNull();
|
expect(snapshotValue).not.toBeNull();
|
||||||
expect(Object.keys(snapshotValue)).toHaveLength(3);
|
expect(Object.keys(snapshotValue)).toHaveLength(3);
|
||||||
expect(snapshotValue[newResume.id]).toBeTruthy();
|
expect(snapshotValue[newResume.id]).toBeTruthy();
|
||||||
expect(snapshotValue[newResume.id].id).toBe(newResume.id);
|
expect(snapshotValue[newResume.id]).toEqual(newResume);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("triggers 'value' event with resumes when updating a resume", async () => {
|
describe('update function', () => {
|
||||||
const userUid = DatabaseConstants.user1.uid;
|
it('can spy on it', async () => {
|
||||||
|
const referencePath = `${DatabaseConstants.resumesPath}/123456`;
|
||||||
|
const functionSpy = jest.spyOn(
|
||||||
|
FirebaseStub.database().ref(referencePath),
|
||||||
|
'update',
|
||||||
|
);
|
||||||
|
const updateArgument = 'test value 123';
|
||||||
|
|
||||||
|
await FirebaseStub.database().ref(referencePath).update(updateArgument);
|
||||||
|
|
||||||
|
expect(functionSpy).toHaveBeenCalledTimes(1);
|
||||||
|
const functionCallArgument = functionSpy.mock.calls[0][0];
|
||||||
|
expect(functionCallArgument).toBeTruthy();
|
||||||
|
expect(functionCallArgument).toEqual(updateArgument);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('updates data', async () => {
|
||||||
|
const resumeId = DatabaseConstants.demoStateResume1Id;
|
||||||
|
const existingResume = (
|
||||||
|
await FirebaseStub.database()
|
||||||
|
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
|
||||||
|
.once('value')
|
||||||
|
).val();
|
||||||
|
expect(existingResume).toBeTruthy();
|
||||||
|
|
||||||
|
const resumeName = 'Test Resume renamed';
|
||||||
|
existingResume.name = resumeName;
|
||||||
|
await FirebaseStub.database()
|
||||||
|
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
|
||||||
|
.update(existingResume);
|
||||||
|
|
||||||
|
const actualResume = (
|
||||||
|
await FirebaseStub.database()
|
||||||
|
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
|
||||||
|
.once('value')
|
||||||
|
).val();
|
||||||
|
expect(actualResume).toBeTruthy();
|
||||||
|
expect(existingResume).toEqual(actualResume);
|
||||||
|
expect(actualResume.name).toEqual(resumeName);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('triggers events', async () => {
|
||||||
let snapshotValue = null;
|
let snapshotValue = null;
|
||||||
const callback = jest.fn((snapshot) => {
|
const callback = jest.fn((snapshot) => {
|
||||||
snapshotValue = snapshot.val();
|
snapshotValue = snapshot.val();
|
||||||
});
|
});
|
||||||
|
|
||||||
FirebaseStub.database()
|
FirebaseStub.database()
|
||||||
.ref(DatabaseConstants.resumesPath)
|
.ref(DatabaseConstants.resumesPath)
|
||||||
.orderByChild('user')
|
.orderByChild('user')
|
||||||
.equalTo(userUid)
|
.equalTo(DatabaseConstants.user1.uid)
|
||||||
.on('value', callback);
|
.on('value', callback);
|
||||||
|
|
||||||
await waitFor(() => callback.mock.calls[0][0]);
|
await waitFor(() => callback.mock.calls[0][0]);
|
||||||
|
|
||||||
expect(callback.mock.calls).toHaveLength(1);
|
|
||||||
callback.mockClear();
|
callback.mockClear();
|
||||||
expect(snapshotValue).not.toBeNull();
|
|
||||||
expect(Object.keys(snapshotValue)).toHaveLength(2);
|
|
||||||
Object.values(snapshotValue).forEach((resume) =>
|
|
||||||
expect(resume.user).toEqual(userUid),
|
|
||||||
);
|
|
||||||
snapshotValue = null;
|
snapshotValue = null;
|
||||||
|
|
||||||
const existingResume = (
|
const existingResume = (
|
||||||
@ -366,10 +400,8 @@ describe('FirebaseStub', () => {
|
|||||||
.once('value')
|
.once('value')
|
||||||
).val();
|
).val();
|
||||||
expect(existingResume).toBeTruthy();
|
expect(existingResume).toBeTruthy();
|
||||||
expect(existingResume.user).toEqual(userUid);
|
|
||||||
|
|
||||||
existingResume.name = 'Test Resume renamed';
|
existingResume.name = 'Test Resume renamed';
|
||||||
|
|
||||||
await FirebaseStub.database()
|
await FirebaseStub.database()
|
||||||
.ref(`${DatabaseConstants.resumesPath}/${existingResume.id}`)
|
.ref(`${DatabaseConstants.resumesPath}/${existingResume.id}`)
|
||||||
.update(existingResume);
|
.update(existingResume);
|
||||||
@ -380,10 +412,33 @@ describe('FirebaseStub', () => {
|
|||||||
expect(snapshotValue).not.toBeNull();
|
expect(snapshotValue).not.toBeNull();
|
||||||
expect(Object.keys(snapshotValue)).toHaveLength(2);
|
expect(Object.keys(snapshotValue)).toHaveLength(2);
|
||||||
expect(snapshotValue[existingResume.id]).toBeTruthy();
|
expect(snapshotValue[existingResume.id]).toBeTruthy();
|
||||||
expect(snapshotValue[existingResume.id].name).toBe(existingResume.name);
|
expect(snapshotValue[existingResume.id]).toEqual(existingResume);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("triggers 'child_removed' and 'value' events when removing a resume", async () => {
|
describe('remove function', () => {
|
||||||
|
it('deletes data', async () => {
|
||||||
|
const resumeId = DatabaseConstants.demoStateResume1Id;
|
||||||
|
const removedResume = (
|
||||||
|
await FirebaseStub.database()
|
||||||
|
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
|
||||||
|
.once('value')
|
||||||
|
).val();
|
||||||
|
expect(removedResume).toBeTruthy();
|
||||||
|
|
||||||
|
await FirebaseStub.database()
|
||||||
|
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
|
||||||
|
.remove();
|
||||||
|
|
||||||
|
const actualResume = (
|
||||||
|
await FirebaseStub.database()
|
||||||
|
.ref(`${DatabaseConstants.resumesPath}/${resumeId}`)
|
||||||
|
.once('value')
|
||||||
|
).val();
|
||||||
|
expect(actualResume).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('triggers events', async () => {
|
||||||
const userUid = DatabaseConstants.user1.uid;
|
const userUid = DatabaseConstants.user1.uid;
|
||||||
|
|
||||||
let valueCallbackSnapshotValue = null;
|
let valueCallbackSnapshotValue = null;
|
||||||
@ -396,13 +451,7 @@ describe('FirebaseStub', () => {
|
|||||||
.equalTo(userUid)
|
.equalTo(userUid)
|
||||||
.on('value', valueCallback);
|
.on('value', valueCallback);
|
||||||
await waitFor(() => valueCallback.mock.calls[0][0]);
|
await waitFor(() => valueCallback.mock.calls[0][0]);
|
||||||
expect(valueCallback.mock.calls).toHaveLength(1);
|
|
||||||
valueCallback.mockClear();
|
valueCallback.mockClear();
|
||||||
expect(valueCallbackSnapshotValue).not.toBeNull();
|
|
||||||
expect(Object.keys(valueCallbackSnapshotValue)).toHaveLength(2);
|
|
||||||
Object.values(valueCallbackSnapshotValue).forEach((resume) =>
|
|
||||||
expect(resume.user).toEqual(userUid),
|
|
||||||
);
|
|
||||||
valueCallbackSnapshotValue = null;
|
valueCallbackSnapshotValue = null;
|
||||||
|
|
||||||
let childRemovedCallbackSnapshotValue = null;
|
let childRemovedCallbackSnapshotValue = null;
|
||||||
@ -431,15 +480,17 @@ describe('FirebaseStub', () => {
|
|||||||
await waitFor(() => childRemovedCallback.mock.calls[0][0]);
|
await waitFor(() => childRemovedCallback.mock.calls[0][0]);
|
||||||
expect(childRemovedCallback.mock.calls).toHaveLength(1);
|
expect(childRemovedCallback.mock.calls).toHaveLength(1);
|
||||||
expect(childRemovedCallbackSnapshotValue).toBeTruthy();
|
expect(childRemovedCallbackSnapshotValue).toBeTruthy();
|
||||||
expect(childRemovedCallbackSnapshotValue.id).toBe(removedResume.id);
|
expect(childRemovedCallbackSnapshotValue).toEqual(removedResume);
|
||||||
|
|
||||||
await waitFor(() => valueCallback.mock.calls[0][0]);
|
await waitFor(() => valueCallback.mock.calls[0][0]);
|
||||||
expect(valueCallback.mock.calls).toHaveLength(1);
|
expect(valueCallback.mock.calls).toHaveLength(1);
|
||||||
expect(valueCallbackSnapshotValue).toBeTruthy();
|
expect(valueCallbackSnapshotValue).toBeTruthy();
|
||||||
expect(removedResume.id in valueCallbackSnapshotValue).toBe(false);
|
expect(removedResume.id in valueCallbackSnapshotValue).toBe(false);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("does not trigger any event after 'off' is called", async () => {
|
describe('off function', () => {
|
||||||
|
it('removes event callbacks', async () => {
|
||||||
const userUid = DatabaseConstants.user1.uid;
|
const userUid = DatabaseConstants.user1.uid;
|
||||||
|
|
||||||
let valueCallbackSnapshotValue = null;
|
let valueCallbackSnapshotValue = null;
|
||||||
@ -452,7 +503,6 @@ describe('FirebaseStub', () => {
|
|||||||
.equalTo(userUid)
|
.equalTo(userUid)
|
||||||
.on('value', valueCallback);
|
.on('value', valueCallback);
|
||||||
await waitFor(() => valueCallback.mock.calls[0][0]);
|
await waitFor(() => valueCallback.mock.calls[0][0]);
|
||||||
expect(valueCallback.mock.calls).toHaveLength(1);
|
|
||||||
valueCallback.mockClear();
|
valueCallback.mockClear();
|
||||||
valueCallbackSnapshotValue = null;
|
valueCallbackSnapshotValue = null;
|
||||||
|
|
||||||
@ -488,3 +538,4 @@ describe('FirebaseStub', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user