-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Database.Servervalue.increment(x) (#2348)
* Added ServerValue._increment() for not-yet-working operator (needs server-side rollout and API approval) * Changed server value local resolution to include current offline caches * Add new tests for ServerValues in general + offline increments
- Loading branch information
Showing
6 changed files
with
183 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import { getRandomNode } from './helpers/util'; | ||
import { Database } from '../src/api/Database'; | ||
import { Reference } from '../src/api/Reference'; | ||
import { nodeFromJSON } from '../src/core/snap/nodeFromJSON'; | ||
|
||
describe('ServerValue tests', () => { | ||
it('resolves timestamps locally', async () => { | ||
const node = getRandomNode() as Reference; | ||
const start = Date.now(); | ||
const values: Array<number> = []; | ||
node.on('value', snap => { | ||
expect(typeof snap.val()).to.equal('number'); | ||
values.push(snap.val() as number); | ||
}); | ||
await node.set(Database.ServerValue.TIMESTAMP); | ||
node.off('value'); | ||
|
||
// By the time the write is acknowledged, we should have a local and | ||
// server version of the timestamp. | ||
expect(values.length).to.equal(2); | ||
values.forEach(serverTime => { | ||
const delta = Math.abs(serverTime - start); | ||
expect(delta).to.be.lessThan(1000); | ||
}); | ||
}); | ||
|
||
it('handles increments without listeners', () => { | ||
// Ensure that increments don't explode when the SyncTree must return a null | ||
// node (i.e. ChildrenNode.EMPTY_NODE) because there is not yet any synced | ||
// data. | ||
const node = getRandomNode() as Reference; | ||
const addOne = Database.ServerValue._increment(1); | ||
|
||
node.set(addOne); | ||
}); | ||
|
||
it('handles increments locally', async () => { | ||
const node = getRandomNode() as Reference; | ||
const addOne = Database.ServerValue._increment(1); | ||
|
||
// Must go offline because the latest emulator may not support this server op | ||
// This also means we can't await node operations, which would block the test. | ||
node.database.goOffline(); | ||
try { | ||
const values: Array<any> = []; | ||
const expected: Array<any> = []; | ||
node.on('value', snap => values.push(snap.val())); | ||
|
||
// null -> increment(x) = x | ||
node.set(addOne); | ||
expected.push(1); | ||
|
||
// x -> increment(y) = x + y | ||
node.set(5); | ||
node.set(addOne); | ||
expected.push(5); | ||
expected.push(6); | ||
|
||
// str -> increment(x) = x | ||
node.set('hello'); | ||
node.set(addOne); | ||
expected.push('hello'); | ||
expected.push(1); | ||
|
||
// obj -> increment(x) = x | ||
node.set({ 'hello': 'world' }); | ||
node.set(addOne); | ||
expected.push({ 'hello': 'world' }); | ||
expected.push(1); | ||
|
||
node.off('value'); | ||
expect(values).to.deep.equal(expected); | ||
} finally { | ||
node.database.goOnline(); | ||
} | ||
}); | ||
}); |