test: improve overall test coverage

This commit is contained in:
thecodrr
2022-08-15 10:57:25 +05:00
parent 6d45c23e79
commit 87b778db94
25 changed files with 908 additions and 714 deletions

View File

@@ -1,4 +1,8 @@
import { databaseTest } from "./utils";
import { databaseTest, notebookTest, StorageInterface } from "./utils";
beforeEach(() => {
StorageInterface.clear();
});
test("settings' dateModified should not update on init", () =>
databaseTest().then(async (db) => {
@@ -29,3 +33,75 @@ test("tag alias should update if aliases in settings update", () =>
});
expect(db.tags.tag(tag.id).alias).toBe("hello232");
}));
test("save group options", () =>
databaseTest().then(async (db) => {
const groupOptions = {
groupBy: "abc",
sortBy: "dateCreated",
sortDirection: "asc",
};
await db.settings.setGroupOptions("home", groupOptions);
expect(db.settings.getGroupOptions("home")).toMatchObject(groupOptions);
}));
test("save toolbar config", () =>
databaseTest().then(async (db) => {
const toolbarConfig = {
preset: "custom",
config: ["bold", "italic"],
};
await db.settings.setToolbarConfig("mobile", toolbarConfig);
expect(db.settings.getToolbarConfig("mobile")).toMatchObject(toolbarConfig);
}));
test("pinning an invalid item should throw", () =>
databaseTest().then(async (db) => {
await expect(() => db.settings.pin("lolo", {})).rejects.toThrow(
/item cannot be pinned/i
);
}));
test("pin a notebook", () =>
notebookTest().then(async ({ db, id }) => {
await db.settings.pin("notebook", { id });
expect(db.settings.pins).toHaveLength(1);
expect(db.settings.pins[0].id).toBe(id);
}));
test("pin an already pinned notebook", () =>
notebookTest().then(async ({ db, id }) => {
await db.settings.pin("notebook", { id });
await db.settings.pin("notebook", { id });
expect(db.settings.pins).toHaveLength(1);
expect(db.settings.pins[0].id).toBe(id);
}));
test("pin a topic", () =>
notebookTest().then(async ({ db, id }) => {
const notebook = db.notebooks.notebook(id)._notebook;
const topic = notebook.topics[0];
await db.settings.pin("topic", { id: topic.id, notebookId: id });
expect(db.settings.pins).toHaveLength(1);
expect(db.settings.pins[0].id).toBe(topic.id);
}));
test("pin a tag", () =>
databaseTest().then(async (db) => {
const tag = await db.tags.add("HELLO!");
await db.settings.pin("tag", { id: tag.id });
expect(db.settings.pins).toHaveLength(1);
expect(db.settings.pins[0].id).toBe(tag.id);
}));
test("unpin a pinned item", () =>
databaseTest().then(async (db) => {
const tag = await db.tags.add("HELLO!");
await db.settings.pin("tag", { id: tag.id });
expect(db.settings.pins).toHaveLength(1);
expect(db.settings.pins[0].id).toBe(tag.id);
await db.settings.unpin(tag.id);
expect(db.settings.pins).toHaveLength(0);
}));

View File

@@ -1,148 +0,0 @@
import { enableFetchMocks } from "jest-fetch-mock";
import { StorageInterface, databaseTest } from "./utils";
const SUCCESS_LOGIN_RESPONSE = {
access_token: "access_token",
refresh_token: "refresh_token",
scope: "sync",
expires_in: 3600,
};
const SUCCESS_USER_RESPONSE = {
id: "0",
email: process.env.EMAIL,
salt: "",
};
function mock(expiry = 3600) {
fetch
.mockResponseOnce(
JSON.stringify({ ...SUCCESS_LOGIN_RESPONSE, expires_in: expiry }),
{
headers: { "Content-Type": "application/json" },
}
)
.mockResponseOnce(JSON.stringify(SUCCESS_USER_RESPONSE), {
headers: { "Content-Type": "application/json" },
});
}
beforeAll(() => {
enableFetchMocks();
});
beforeEach(() => {
fetch.resetMocks();
StorageInterface.clear();
});
test("no user should be returned when not logged in", () =>
databaseTest().then(async (db) => {
expect(await db.user.getUser()).toBeUndefined();
}));
test("undefined user should not be set", () =>
databaseTest().then(async (db) => {
await db.user.setUser();
expect(await db.user.getUser()).toBeUndefined();
}));
test("login user", async () =>
databaseTest().then(async (db) => {
mock();
await db.user.login("myuser", "mylogin", true, "mylogin");
const dbuser = await db.user.getUser();
expect(dbuser.email).toBe(SUCCESS_USER_RESPONSE.email);
expect(dbuser.id).toBe(SUCCESS_USER_RESPONSE.id);
}));
test("login user with wrong password", () =>
databaseTest().then(async (db) => {
fetch.mockResponseOnce(
JSON.stringify({
error_description: "Username or password is incorrect.",
}),
{ status: 400, headers: { "Content-Type": "application/json" } }
);
await expect(
db.user.login("myuser", "wrongpassword", true, "wrongpassword")
).rejects.toThrow(/Username or password is incorrect./);
}));
test("failed login with unknown error", () =>
databaseTest().then(async (db) => {
fetch.mockResponseOnce(JSON.stringify({}), { status: 400 });
await expect(
db.user.login("myuser", "wrongpassword", true, "wrongpassword")
).rejects.toThrow(/Request failed with status code: /);
}));
test("signup user", () =>
databaseTest().then(async (db) => {
fetch.mockResponseOnce(undefined, { status: 200 });
mock();
await db.user.signup(SUCCESS_USER_RESPONSE.email, "password");
const dbuser = await db.user.getUser();
expect(dbuser.email).toBe(SUCCESS_USER_RESPONSE.email);
}));
test("logout user", () =>
databaseTest().then(async (db) => {
mock();
await db.user.login("myuser", "mylogin", true, "mylogin");
const dbuser = await db.user.getUser();
expect(dbuser.email).toBe(SUCCESS_USER_RESPONSE.email);
await db.user.logout();
expect(await db.user.getUser()).toBeUndefined();
}));
test("refresh user's token", () =>
databaseTest().then(async (db) => {
mock();
await db.user.login(
SUCCESS_USER_RESPONSE.email,
"mylogin",
true,
"mylogin"
);
const token = await db.user.tokenManager.getToken();
await db.user.tokenManager.saveToken({ ...token, expires_in: -2000 });
fetch.mockResponseOnce(
JSON.stringify({
...SUCCESS_LOGIN_RESPONSE,
access_token: "new_token",
refresh_token: "new_refresh_token",
expires_in: 3600,
}),
{
headers: { "Content-Type": "application/json" },
}
);
const { access_token, refresh_token } =
await db.user.tokenManager.getToken();
expect(refresh_token).toBe("new_refresh_token");
expect(access_token).toBe("new_token");
}));
test("refresh user's token when its not expired", () =>
databaseTest().then(async (db) => {
mock();
await db.user.login(
SUCCESS_USER_RESPONSE.email,
"mylogin",
true,
"mylogin"
);
expect(await db.user.tokenManager.getAccessToken()).toBe("access_token");
const dbuser = await db.user.getUser();
expect(dbuser.email).toBe(SUCCESS_USER_RESPONSE.email);
}));
test("refresh token for non existent user should do nothing", () =>
databaseTest().then(async (db) => {
fetch.mockResponseOnce(JSON.stringify(SUCCESS_LOGIN_RESPONSE));
expect(await db.user.tokenManager.getAccessToken()).toBeUndefined();
const dbuser = await db.user.getUser();
expect(dbuser).toBeUndefined();
}));