mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-14 16:51:33 +10:00
Firebase mock: mock auth() signIn, in order to trigger onAuthStateChanged
This commit is contained in:
@ -1 +1,2 @@
|
|||||||
module.exports = 'test-file-stub';
|
const mockFile = 'test-file-stub';
|
||||||
|
export default mockFile;
|
||||||
|
|||||||
@ -6,9 +6,16 @@ const emptyResumeId = 'mtre01';
|
|||||||
let resumesDictionary = {};
|
let resumesDictionary = {};
|
||||||
let useDemoResume = false;
|
let useDemoResume = false;
|
||||||
|
|
||||||
|
let onAuthStateChangedObservers = [];
|
||||||
|
const testUser = {
|
||||||
|
displayName: 'Test User',
|
||||||
|
email: 'noemail@noemail.com',
|
||||||
|
};
|
||||||
|
|
||||||
const __init = () => {
|
const __init = () => {
|
||||||
resumesDictionary = {};
|
resumesDictionary = {};
|
||||||
useDemoResume = false;
|
useDemoResume = false;
|
||||||
|
onAuthStateChangedObservers = [];
|
||||||
|
|
||||||
const demoResume = __readFile('../src/data/demoState.json');
|
const demoResume = __readFile('../src/data/demoState.json');
|
||||||
resumesDictionary[demoResumeId] = demoResume;
|
resumesDictionary[demoResumeId] = demoResume;
|
||||||
@ -48,18 +55,34 @@ const __getResumeId = () => {
|
|||||||
return __getResume().id;
|
return __getResume().id;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onAuthStateChanged = (observer) => {
|
||||||
|
onAuthStateChangedObservers.push(observer);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
onAuthStateChangedObservers = onAuthStateChangedObservers.filter(
|
||||||
|
(observer) => observer !== observer,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const signInAnonymously = async () => {
|
||||||
|
onAuthStateChangedObservers.forEach((observer) => observer(testUser));
|
||||||
|
|
||||||
|
var result = await Promise.resolve(testUser);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
database: jest.fn().mockReturnValue({
|
database: jest.fn().mockReturnValue({
|
||||||
ref: jest.fn().mockReturnValue({
|
ref: jest.fn().mockReturnValue({
|
||||||
once: jest.fn().mockResolvedValue({
|
once: jest.fn().mockResolvedValue({
|
||||||
val: jest.fn().mockImplementation(() => {
|
val: jest.fn(__getResume),
|
||||||
return __getResume();
|
|
||||||
}),
|
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
auth: jest.fn().mockReturnValue({
|
auth: jest.fn().mockReturnValue({
|
||||||
onAuthStateChanged: jest.fn().mockReturnValue(jest.fn()),
|
onAuthStateChanged: onAuthStateChanged,
|
||||||
|
signInAnonymously: signInAnonymously,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { render, cleanup } from '@testing-library/react';
|
import { render, cleanup } from '@testing-library/react';
|
||||||
|
|
||||||
import {
|
import firebase, {
|
||||||
__init as firebaseMockInit,
|
__init as firebaseMockInit,
|
||||||
__useDemoResume as firebaseMockUseDemoResume,
|
__useDemoResume as firebaseMockUseDemoResume,
|
||||||
__getResumeId as firebaseMockGetResumeId,
|
__getResumeId as firebaseMockGetResumeId,
|
||||||
@ -36,6 +36,8 @@ it('renders correctly', async () => {
|
|||||||
</UserProvider>,
|
</UserProvider>,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await firebase.auth().signInAnonymously();
|
||||||
|
|
||||||
expect(container).toBeTruthy();
|
expect(container).toBeTruthy();
|
||||||
//expect(container).toBeInTheDocument();
|
//expect(container).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user