Skip to content

Commit

Permalink
fix(connect-user): Use StubCreator to mock firebase during tests
Browse files Browse the repository at this point in the history
By using StubCreator, the connect-user module can properly import the FirebaseClient. Importing
using require does not work once compiled. This fixes a 500 error on Firebase where the
firebaseClient module could not be found in connectUser
  • Loading branch information
nielsmaerten committed Sep 10, 2017
1 parent b5e9d5e commit de57029
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/eventhandlers/connect-user.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Config from "../objects/config"
import User from "../objects/user"
import FirebaseClient from "../clients/firebase-client"

export default async (req: any): Promise<string> => {
const request = require("request-promise")
const functions = require("firebase-functions")
const FirebaseClient = require("../clients/firebase-client")
const firebaseConfig =
(global as any).peppermintFirebaseConfig || functions.config()

Expand Down
26 changes: 4 additions & 22 deletions test/eventhandlers/connect-user.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Peppermint from "../../src/peppermint"
import StubCreator from "../helpers/stub-creator"
import { assert } from "chai"

describe("Peppermint.connectUser", () => {
Expand All @@ -8,30 +9,11 @@ describe("Peppermint.connectUser", () => {
post: jest.fn().mockReturnValue({ access_token: "" })
}
})
jest.mock("firebase-functions", () => {
return {
config: () => {
return {
dropbox: {
client_id: "XXX",
client_secret: "XXX"
},
oauth: {
redirect_after_connect: "https://google.com"
}
}
}
}
})
jest.mock("../../src/clients/firebase-client", () => {
return {
getInstance: jest.fn().mockReturnValue({
addUser: jest.fn()
})
}
})
StubCreator.stubFirebase()
})

afterEach(StubCreator.restoreFirebase)

it("should exchange an authCode for an access token", async () => {
let authUrl = await Peppermint.connectUser({
query: { code: "FAKE_AUTH_CODE" }
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/stub-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export default class StubCreator {
// Fake urls, because Firebase complains if we leave them undefined
databaseURL: "https://mock-firebase.firebaseio.com",
storageBucket: "mock-firebase.appspot.com"
}
},
dropbox: {},
oauth: {}
})

// Create stub firebase database and enable autoflushing
Expand Down

0 comments on commit de57029

Please sign in to comment.