Skip to content

Commit b12a61f

Browse files
committed
WIP: add database.test.ts
1 parent 7f78f89 commit b12a61f

File tree

4 files changed

+105
-6
lines changed

4 files changed

+105
-6
lines changed

src/database.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ declare module './app/firebase_app' {
5858

5959
declare module './app/firebase_app' {
6060
interface FirebaseNamespace {
61-
database?(app: FirebaseApp): Database
61+
database?: {
62+
(app?: FirebaseApp): Database,
63+
Reference,
64+
Query,
65+
Database,
66+
enableLogging,
67+
INTERNAL,
68+
}
6269
}
6370
}
6471

src/database/api/Database.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ export class Database {
4747

4848
/**
4949
* Returns a reference to the root or the path specified in opt_pathString.
50-
* @param {string=} opt_pathString
50+
* @param {string=} pathString
5151
* @return {!Firebase} Firebase reference.
5252
*/
53-
ref(opt_pathString): Reference {
53+
ref(pathString?: string): Reference {
5454
this.checkDeleted_('ref');
5555
validateArgCount('database.ref', 0, 1, arguments.length);
5656

57-
return opt_pathString !== undefined ? this.root_.child(opt_pathString) : this.root_;
57+
return pathString !== undefined ? this.root_.child(pathString) : this.root_;
5858
}
5959

6060
/**

tests/database/database.test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { expect } from "chai";
2+
import firebase from "../../src/app";
3+
import {
4+
TEST_PROJECT,
5+
patchFakeAuthFunctions,
6+
} from "./helpers";
7+
import "../../src/database";
8+
9+
describe.only('Database Tests', function() {
10+
var defaultApp;
11+
12+
beforeEach(function() {
13+
defaultApp = firebase.initializeApp({databaseURL: TEST_PROJECT.databaseURL});
14+
patchFakeAuthFunctions(defaultApp);
15+
});
16+
17+
afterEach(function() {
18+
return defaultApp.delete();
19+
});
20+
21+
it('Can get database.', function() {
22+
var db = firebase.database();
23+
expect(db).to.not.be.undefined;
24+
expect(db).not.to.be.null;
25+
});
26+
27+
it('Illegal to call constructor', function() {
28+
expect(function() {
29+
var db = new firebase.database.Database('url');
30+
}).to.throw(/don't call new Database/i);
31+
});
32+
33+
it('Can get app', function() {
34+
var db = firebase.database();
35+
expect(db.app).to.not.be.undefined;
36+
expect((db.app as any) instanceof firebase.app.App);
37+
});
38+
39+
it('Can get root ref', function() {
40+
var db = firebase.database();
41+
42+
var ref = db.ref();
43+
44+
expect(ref instanceof firebase.database.Reference).to.be.true;
45+
expect(ref.key).to.be.null;
46+
});
47+
48+
it('Can get child ref', function() {
49+
var db = firebase.database();
50+
51+
var ref = db.ref('child');
52+
53+
expect(ref instanceof firebase.database.Reference).to.be.true;
54+
expect(ref.key).to.equal('child');
55+
});
56+
57+
it('Can get deep child ref', function() {
58+
var db = firebase.database();
59+
60+
var ref = db.ref('child/grand-child');
61+
62+
expect(ref instanceof firebase.database.Reference).to.be.true;
63+
expect(ref.key).to.equal('grand-child');
64+
});
65+
66+
it('ref() validates arguments', function() {
67+
var db = firebase.database();
68+
expect(function() {
69+
var ref = (db as any).ref('path', 'extra');
70+
}).to.throw(/Expects no more than 1/);
71+
});
72+
73+
it('Can get refFromURL()', function() {
74+
var db = firebase.database();
75+
var ref = db.refFromURL(TEST_PROJECT.databaseURL + '/path/to/data');
76+
expect(ref.key).to.equal('data');
77+
});
78+
79+
it('refFromURL() validates domain', function() {
80+
var db = firebase.database();
81+
expect(function() {
82+
var ref = db.refFromURL('https://thisisnotarealfirebase.firebaseio.com/path/to/data');
83+
}).to.throw(/does not match.*database/i);
84+
});
85+
86+
it('refFromURL() validates argument', function() {
87+
var db = firebase.database();
88+
expect(function() {
89+
var ref = (db as any).refFromURL();
90+
}).to.throw(/Expects at least 1/);
91+
});
92+
});

tests/database/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Reference } from "../../src/database/api/Reference";
44
import { Query } from "../../src/database/api/Query";
55
import { expect } from "chai";
66

7-
const TEST_PROJECT = require('../config/project.json');
7+
export const TEST_PROJECT = require('../config/project.json');
88

99
var qs = {};
1010
if ('location' in this) {
@@ -22,7 +22,7 @@ let numDatabases = 0;
2222
* @param {!FirebaseApp} app
2323
* @return {!FirebaseApp}
2424
*/
25-
function patchFakeAuthFunctions(app) {
25+
export function patchFakeAuthFunctions(app) {
2626
var token_ = null;
2727

2828
app['INTERNAL'] = app['INTERNAL'] || {};

0 commit comments

Comments
 (0)