Skip to content

Commit

Permalink
Enabled devs to login, logout and CRUD against FireBase in Cypress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhung88 committed Oct 3, 2019
1 parent 13d858d commit b441699
Show file tree
Hide file tree
Showing 8 changed files with 433 additions and 180 deletions.
9 changes: 6 additions & 3 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"linters": {
"*.{ts,tsx}": [
"tslint --project tsconfig.json --config tslint.json"
"src/**/*.{ts,tsx}": [
"tslint --project tsconfig.json --config tslint.json"
],
"cypress/**/*.{ts,tsx}": [
"tslint --project cypress/tsconfig.json --config tslint.json"
],
"*.{js,jsx}": [
"eslint"
Expand All @@ -12,4 +15,4 @@
]
},
"ignore": ["functions/**",".storybook/**",config/**]
}
}
3 changes: 2 additions & 1 deletion cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"reporter": "junit",
"reporterOptions": {
"mochaFile": "cypress/coverage/out-[hash].xml"
}
},
"supportFile": "cypress/support/index.ts"
}
36 changes: 36 additions & 0 deletions cypress/integration/firebase-integration.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
describe('FireBase + Cypress', () => {
it('should logout and login', () => {
cy.visit('/how-to')
cy.logout()
cy.get('[data-cy=create]').should('not.exist')

cy.login('test1234@test.com', '12345678')
cy.get('[data-cy=create]').should('be.visible')

cy.logout()
cy.get('[data-cy=create]').should('not.exist')
})

it('should add/update/get/delete from FireStore', async () => {
const firestore = await cy.firestore()
const addRef = await firestore
.collection('testCollection')
.add({ name: 'Tony Stark', age: 45 })
console.log('Object added with ref ' + addRef.id)
await firestore
.collection('testCollection')
.doc(addRef.id)
.update({ name: 'Iron man', age: 40 })
console.log('Object updated ')
const snapshot = await firestore.collection('testCollection').get()
snapshot.forEach(doc => {
console.log(`ID ${doc.id} => `, doc.data())
})
await firestore
.collection('testCollection')
.doc(addRef.id)
.delete()
console.log(`Object ${addRef.id} deleted`)
cy.visit('/how-to')
})
})
25 changes: 0 additions & 25 deletions cypress/support/commands.js

This file was deleted.

70 changes: 70 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/database'
import 'firebase/firestore'
import cypress from 'cypress'

const projectId = 'precious-plastics-v4-dev'
const apiKey = 'AIzaSyChVNSMiYxCkbGd9C95aChr9GxRJtW6NRA'
const fbConfig = {
apiKey,
authDomain: `${projectId}.firebaseapp.com`,
databaseURL: `https://${projectId}.firebaseio.com`,
projectId: `${projectId}`,
storageBucket: `${projectId}.appspot.com`,
messagingSenderId: '174193431763',
}
firebase.initializeApp(fbConfig)

declare global {
namespace Cypress {
// tslint:disable-next-line:interface-name
interface Chainable {
firestore(): Promise<firebase.firestore.Firestore>

login(
username: string,
password: string,
): Promise<firebase.auth.UserCredential>

logout(): Promise<void>
}
}
}

const attachCustomCommands = (
Cypress,
{ auth, firestore }: typeof firebase,
) => {
let currentUser: null | firebase.User = null
auth().onAuthStateChanged(user => {
currentUser = user
})

Cypress.Commands.add('login', (email, password) => {
Cypress.log({
displayName: 'login',
consoleProps: () => {
return { email, password }
},
})
return auth().signInWithEmailAndPassword(email, password)
})

Cypress.Commands.add('logout', () => {
const userInfo = currentUser ? currentUser.email : 'Not login yet - Skipped'
Cypress.log({
displayName: 'logout',
consoleProps: () => {
return { currentUser: userInfo }
},
})
return auth().signOut()
})

Cypress.Commands.add('firestore', () => {
return firestore()
})
}

attachCustomCommands(Cypress, firebase)
1 change: 1 addition & 0 deletions cypress/support/index.js → cypress/support/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './commands'
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"final-form": "4.18.5",
"final-form-arrays": "^3.0.1",
"final-form-calculate": "^1.3.1",
"firebase": "5.8.1",
"firebase": "^7.0.0",
"fork-ts-checker-webpack-plugin-alt": "0.4.14",
"fs-extra": "7.0.1",
"geolocation-utils": "^1.2.1",
Expand Down

0 comments on commit b441699

Please sign in to comment.