Skip to content

Commit

Permalink
Fix tests after server dates updae
Browse files Browse the repository at this point in the history
  • Loading branch information
kossnocorp committed Jun 22, 2021
1 parent 4536909 commit d5b131a
Show file tree
Hide file tree
Showing 25 changed files with 4,673 additions and 4,059 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 14.15.4
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ See below for details.

- **BREAKING**: Moved `onMissing` to options in `getMany`.

- `TransactionWriteFunction` no longer expect to return a promise.

## 7.2.0 - 2021-05-25

- Replace deprecated `@firebase/rules-testing` with `@firebase/rules-unit-testing`.
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const webpack = require('webpack')

process.env.CHROME_BIN = require('puppeteer').executablePath()

module.exports = config => {
module.exports = (config) => {
config.set({
files: ['test/karmaTests.ts'],

Expand Down
40 changes: 21 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,37 @@
"author": "Sasha Koss <[email protected]>",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.4.5",
"@babel/core": "^7.12.10",
"@babel/plugin-proposal-optional-chaining": "^7.12.7",
"@babel/preset-env": "^7.4.5",
"@babel/preset-typescript": "^7.3.3",
"@babel/preset-env": "^7.12.11",
"@babel/preset-typescript": "^7.12.7",
"@firebase/rules-unit-testing": "^1.2.10",
"@types/jest": "^24.0.13",
"@types/jest": "^26.0.20",
"@types/node": "^12.0.4",
"@types/sinon": "^7.0.13",
"@types/webpack-env": "^1.14.0",
"babel-loader": "^8.0.6",
"@types/sinon": "^9.0.10",
"@types/webpack-env": "^1.16.0",
"babel-jest": "^26.6.3",
"babel-loader": "^8.2.2",
"babel-preset-power-assert": "^3.0.0",
"firebase": "7.6.1",
"firebase-admin": "8.9.0",
"firebase-tools": "^7.11.0",
"jest": "^24.8.0",
"karma": "^4.2.0",
"karma-chrome-launcher": "^3.0.0",
"karma-jasmine": "^2.0.1",
"firebase": "8.2.5",
"firebase-admin": "9.4.2",
"firebase-tools": "^9.3.0",
"jest": "^26.6.3",
"js-fns": "^2.5.1",
"karma": "^6.0.4",
"karma-chrome-launcher": "^3.1.0",
"karma-jasmine": "^4.0.1",
"karma-mocha": "^1.3.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^4.0.2",
"karma-sourcemap-loader": "^0.3.8",
"karma-webpack": "4",
"mocha": "^6.2.0",
"nanoid": "^3.1.20",
"power-assert": "^1.6.1",
"prettier": "^2.0.4",
"puppeteer": "^1.19.0",
"sinon": "^7.3.2",
"puppeteer": "^6.0.0",
"sinon": "^9.2.4",
"typedoc": "^0.15.0",
"typescript": "4.1.3",
"webpack": "^4.37.0"
"webpack": "4"
}
}
3 changes: 2 additions & 1 deletion src/adaptor/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Browser Firestore adaptor.
*/

import * as firebase from 'firebase/app'
import firebase from 'firebase/app'
import 'firebase/firestore'
import type { DocOptions, ServerTimestampsStrategy } from '../../types'
import { getAll } from '../utils'
Expand All @@ -22,6 +22,7 @@ export default async function adaptor() {
FieldPath: firebase.firestore.FieldPath,
FieldValue: firebase.firestore.FieldValue
},
environment: 'web',
getDocMeta: (snapshot: firebase.firestore.DocumentSnapshot) => ({
fromCache: snapshot.metadata.fromCache,
hasPendingWrites: snapshot.metadata.hasPendingWrites
Expand Down
1 change: 1 addition & 0 deletions src/adaptor/browser/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default async function adaptor() {
FieldPath: firebase.firestore.FieldPath,
FieldValue: firebase.firestore.FieldValue
},
environment: 'web',
getDocMeta: (snapshot: firebase.firestore.DocumentSnapshot) => ({
fromCache: snapshot.metadata.fromCache,
hasPendingWrites: snapshot.metadata.hasPendingWrites
Expand Down
10 changes: 5 additions & 5 deletions src/add/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import add from '.'
import { collection } from '../collection'
import get from '../get'
import { Ref, ref } from '../ref'
import { ServerDate, value } from '../value'
import { ServerDate } from '../types'
import { value } from '../value'

describe('add', () => {
type User = { name: string }
Expand Down Expand Up @@ -77,18 +78,18 @@ describe('add', () => {

it('allows to assert environment which allows setting dates', async () => {
// TODO: Find a way to make the error show on fields like when assertEnvironment: 'web'
// @ts-expect-error
await add(users, nanoid(), {
await add(users, {
name: 'Sasha',
// @ts-expect-error
createdAt: new Date(),
// @ts-expect-error
updatedAt: new Date(),
birthday: new Date(1987, 1, 11)
})

const nodeAdd = () =>
add(
users,
nanoid(),
{
name: 'Sasha',
createdAt: new Date(),
Expand All @@ -101,7 +102,6 @@ describe('add', () => {
const webAdd = () =>
add(
users,
nanoid(),
{
name: 'Sasha',
// @ts-expect-error
Expand Down
6 changes: 3 additions & 3 deletions src/all/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('all', () => {
const orderedBooks = await Promise.all(
docs.map((doc) => get(books, doc.data.book.id))
)
assert.deepEqual(orderedBooks.map(({ data: { title } }) => title).sort(), [
assert.deepEqual(orderedBooks.map((doc) => doc?.data.title).sort(), [
'Sapiens',
'The 22 Immutable Laws of Marketing'
])
Expand All @@ -59,8 +59,8 @@ describe('all', () => {
set(orders, 'order2', { book: ref(books, '22laws'), quantity: 1, date })
])
const docs = await all(orders)
assert(docs[0].data.date.getTime() === date.getTime())
assert(docs[1].data.date.getTime() === date.getTime())
assert(docs[0]?.data.date?.getTime() === date.getTime())
assert(docs[1]?.data.date?.getTime() === date.getTime())
})

it('allows to get all data from collection groups', async () => {
Expand Down
29 changes: 15 additions & 14 deletions src/batch/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import assert from 'assert'
import { pick } from 'js-fns'
import { nanoid } from 'nanoid'
import { batch } from '.'
import { collection } from '../collection'
import { ref } from '../ref'
import { nanoid } from 'nanoid'
import get from '../get'
import { ref } from '../ref'
import set from '../set'

describe('batch', () => {
Expand All @@ -25,9 +26,9 @@ describe('batch', () => {
get(tatiRef),
get(edRef)
])
assert(sasha.data.name === 'Sasha')
assert(tati.data.name === 'Tati')
assert(ed.data.name === 'Ed')
assert(sasha!.data.name === 'Sasha')
assert(tati!.data.name === 'Tati')
assert(ed!.data.name === 'Ed')
})

it('allows set a new document', async () => {
Expand All @@ -45,17 +46,17 @@ describe('batch', () => {
get(tatiRef),
get(edRef)
])
assert.deepEqual(sasha, {
assert.deepEqual(pick(sasha!, ['__type__', 'ref', 'data']), {
__type__: 'doc',
ref: { __type__: 'ref', collection: users, id: `${id}-sasha` },
data: { name: 'Sasha' }
})
assert.deepEqual(tati, {
assert.deepEqual(pick(tati!, ['__type__', 'ref', 'data']), {
__type__: 'doc',
ref: { __type__: 'ref', collection: users, id: `${id}-tati` },
data: { name: 'Tati' }
})
assert.deepEqual(ed, {
assert.deepEqual(pick(ed!, ['__type__', 'ref', 'data']), {
__type__: 'doc',
ref: { __type__: 'ref', collection: users, id: `${id}-ed` },
data: { name: 'Ed' }
Expand All @@ -82,9 +83,9 @@ describe('batch', () => {
get(tatiRef),
get(edRef)
])
assert.deepEqual(sasha.data, { name: 'Sasha Koss', foo: true })
assert.deepEqual(tati.data, { name: 'Tati Shepeleva', foo: false })
assert.deepEqual(ed.data, { name: 'Ed Tsech', foo: true })
assert.deepEqual(sasha!.data, { name: 'Sasha Koss', foo: true })
assert.deepEqual(tati!.data, { name: 'Tati Shepeleva', foo: false })
assert.deepEqual(ed!.data, { name: 'Ed Tsech', foo: true })
})

it('allows updating', async () => {
Expand All @@ -107,9 +108,9 @@ describe('batch', () => {
get(tatiRef),
get(edRef)
])
assert(sasha.data.name === 'Sasha Koss')
assert(tati.data.name === 'Tati Shepeleva')
assert(ed.data.name === 'Ed Tsech')
assert(sasha!.data.name === 'Sasha Koss')
assert(tati!.data.name === 'Tati Shepeleva')
assert(ed!.data.name === 'Ed Tsech')
})

it('allows removing', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/doc/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Doc', () => {
__type__: 'doc',
ref: userRef,
data: { name: 'Sasha', createdAt, birthday },
meta: { environment: 'node' }
environment: 'node'
}
)
})
Expand Down
2 changes: 1 addition & 1 deletion src/getMany/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default async function getMany<
): Promise<AnyDoc<Model, RuntimeEnvironment, boolean, ServerTimestamps>[]> {
const a = await adaptor()

assertEnvironment(a, options.assertEnvironment)
assertEnvironment(a, options?.assertEnvironment)

if (ids.length === 0) {
// Firestore#getAll doesn't like empty lists
Expand Down
16 changes: 7 additions & 9 deletions src/getMany/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,22 @@ describe('getMany', () => {
.then(() => {
throw new Error('The promise should be rejected')
})
.catch(err => {
.catch((err) => {
expect(err.message).toBe('Missing document with id nonexistant')
}))

it('allows to specify custom logic when a document is not found', async () => {
const list = await getMany(fruits, ['nonexistant'], id => ({
color: `${id} is missing but I filled it in`
}))
const list = await getMany(fruits, ['nonexistant'], {
onMissing: (id) => ({ color: `${id} is missing but I filled it in` })
})
expect(list.length).toBe(1)
expect(list[0].data.color).toBe('nonexistant is missing but I filled it in')
})

it('allows to ignore missing documents', async () => {
const list = await getMany(
fruits,
['apple', 'nonexistant', 'banana'],
'ignore'
)
const list = await getMany(fruits, ['apple', 'nonexistant', 'banana'], {
onMissing: 'ignore'
})
expect(list.length).toBe(2)
})
})
24 changes: 12 additions & 12 deletions src/onAll/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ describe('onAll', () => {
return new Promise((resolve) => {
const spy = sinon.spy()
off = onAll(orders, async (docs) => {
off()
off?.()
const orderedBooks = await Promise.all(
docs.map((doc) => get(books, doc.data.book.id))
)
spy(orderedBooks.map(({ data: { title } }) => title).sort())
spy(orderedBooks.map((doc) => doc?.data.title).sort())
if (
spy.calledWithMatch(['Sapiens', 'The 22 Immutable Laws of Marketing'])
)
resolve()
resolve(void 0)
})
})
})
Expand All @@ -84,7 +84,7 @@ describe('onAll', () => {
return new Promise((resolve) => {
off = onAll(orders, (docs) => {
if (docs.length === 2 && docs[0].data.date && docs[1].data.date) {
off()
off?.()
if (typeof window === undefined) {
assert(docs[0].data.date.getTime() === date.getTime())
assert(docs[1].data.date.getTime() === date.getTime())
Expand All @@ -93,7 +93,7 @@ describe('onAll', () => {
assert(docs[0].data.date.getTime() - date.getTime() < 20000)
assert(docs[1].data.date.getTime() - date.getTime() < 20000)
}
resolve()
resolve(void 0)
}
})
})
Expand Down Expand Up @@ -127,13 +127,13 @@ describe('onAll', () => {
return new Promise((resolve) => {
off = onAll(allComments, (comments) => {
if (comments.length === 3) {
off()
off?.()
assert.deepEqual(comments.map((c) => c.data.text).sort(), [
'cruel',
'hello',
'world'
])
resolve()
resolve(void 0)
}
})
})
Expand All @@ -143,8 +143,8 @@ describe('onAll', () => {
it('should notify with values all indicate empty', (done) => {
off = onAll(collection('void'), (docs, { changes, empty }) => {
expect(empty).toBeTruthy()
expect(docs).toHaveLength(0)
expect(changes()).toHaveLength(0)
assert(docs.length === 0)
expect(changes().length === 0)
done()
})
})
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('onAll', () => {
const titles = docs.map(({ data: { title } }) => title).sort()
spy(titles)
if (titles.length === 5) {
off()
off?.()
assert(
spy.neverCalledWithMatch([
"Harry Potter and the Sorcerer's Stone",
Expand All @@ -218,12 +218,12 @@ describe('onAll', () => {
'The Mom Test'
])
)
resolve()
resolve(void 0)
}
})
}
on()
off()
off?.()
await set(books, 'hp1', {
title: "Harry Potter and the Sorcerer's Stone"
})
Expand Down
Loading

0 comments on commit d5b131a

Please sign in to comment.