Skip to content

Commit e7d45a5

Browse files
committed
✅ test(Connectioon): added a basic test for Connection class
1 parent de392a8 commit e7d45a5

File tree

7 files changed

+83
-1
lines changed

7 files changed

+83
-1
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
lib/
2+
lib/
3+
**/*.log

Diff for: FirestoreEmulator/.firebaserc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

Diff for: FirestoreEmulator/firebase.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"firestore": {
3+
"rules": "firestore.rules",
4+
"indexes": "firestore.indexes.json"
5+
},
6+
"emulators": {
7+
"firestore": {
8+
"port": 8080
9+
},
10+
"ui": {
11+
"enabled": true,
12+
"port": 8000
13+
}
14+
}
15+
}

Diff for: FirestoreEmulator/firestore.indexes.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"indexes": [],
3+
"fieldOverrides": []
4+
}

Diff for: FirestoreEmulator/firestore.rules

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
service cloud.firestore {
2+
match /databases/{database}/documents {
3+
match /{document=**} {
4+
// This rule allows anyone with your database reference to view, edit,
5+
// and delete all data in your database. It is useful for getting
6+
// started, but it is configured to expire after 30 days because it
7+
// leaves your app open to attackers. At that time, all client
8+
// requests to your database will be denied.
9+
//
10+
// Make sure to write security rules for your app before that time, or
11+
// else all client requests to your database will be denied until you
12+
// update your rules.
13+
allow read, write: if request.time < timestamp.date(2021, 8, 22);
14+
}
15+
}
16+
}

Diff for: jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
preset: "ts-jest",
33
testEnvironment: "node",
4+
testMatch: ["**/__tests__/**/*.test.[jt]s?(x)"],
45
};

Diff for: src/__tests__/Connection/class.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import admin from "firebase-admin";
2+
import Connection from "../../Connection/class";
3+
import * as SchemaType from "../../SchemaTypes";
4+
5+
let firestore: FirebaseFirestore.Firestore;
6+
7+
beforeAll(() => {
8+
process.env["FIRESTORE_EMULATOR_HOST"] = "localhost:8080";
9+
10+
admin.initializeApp({
11+
projectId: "demo-firefly",
12+
});
13+
14+
firestore = admin.firestore();
15+
});
16+
17+
describe("Connects to local emulators", () => {
18+
it("connects to firestore emulator", async () => {
19+
expect.assertions(1);
20+
21+
try {
22+
const connection = new Connection(firestore);
23+
const model = connection.model(
24+
"test",
25+
SchemaType.object<{
26+
hello: string;
27+
}>().keys({
28+
hello: SchemaType.string().required(),
29+
})
30+
);
31+
32+
const doc = await model.create({});
33+
34+
const data = await doc.data();
35+
36+
expect(data).toEqual({
37+
hello: "world",
38+
});
39+
} catch (error) {
40+
const errorType = JSON.parse(error.message).type;
41+
expect(errorType).toBe("Model/SchemaValidation");
42+
}
43+
});
44+
});

0 commit comments

Comments
 (0)