Skip to content

Commit

Permalink
tests(db): test compareScore() and getAreVotedBySessionId() (#300)
Browse files Browse the repository at this point in the history
* tests(db): compareScore and getAreVotedBySessionId

Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com>

* chore: add falsy user tests

Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com>

* tests: `compareScore()` uses array now

Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com>

* tests: check for falsy and wrong session values

Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com>

* tests: use `assertEquals()`

Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com>

---------

Signed-off-by: Niklas Metje <22395665+niklasmtj@users.noreply.github.com>
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
  • Loading branch information
niklasmtj and iuioiua authored Jul 2, 2023
1 parent 849df0f commit 668897a
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions utils/db_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2023 the Deno authors. All rights reserved. MIT license.
import {
type Comment,
compareScore,
createComment,
createItem,
createUser,
Expand All @@ -11,6 +12,7 @@ import {
deleteVote,
formatDate,
getAllItems,
getAreVotedBySessionId,
getCommentsByItem,
getDatesSince,
getItem,
Expand Down Expand Up @@ -236,3 +238,70 @@ Deno.test("[db] getDatesSince()", () => {
formatDate(new Date()),
]);
});

Deno.test("[db] compareScore()", () => {
const item1: Item = {
userId: crypto.randomUUID(),
title: crypto.randomUUID(),
url: `http://${crypto.randomUUID()}.com`,
...newItemProps(),
score: 1,
};
const item2: Item = {
userId: crypto.randomUUID(),
title: crypto.randomUUID(),
url: `http://${crypto.randomUUID()}.com`,
...newItemProps(),
score: 2,
};
const item3: Item = {
userId: crypto.randomUUID(),
title: crypto.randomUUID(),
url: `http://${crypto.randomUUID()}.com`,
...newItemProps(),
score: 5,
};

const aa = [item2, item3, item1];
const sorted = aa.toSorted(compareScore);

assertArrayIncludes(sorted, [item1, item2, item3]);
});

Deno.test("[db] getAreVotedBySessionId()", async () => {
const item: Item = {
userId: crypto.randomUUID(),
title: crypto.randomUUID(),
url: `http://${crypto.randomUUID()}.com`,
...newItemProps(),
score: 1,
};

const user = genNewUser();
const vote = { item, user };

assertEquals(await getUserBySession(user.sessionId), null);
assertEquals(await getItem(item.id), null);
assertEquals(await getAreVotedBySessionId([item], user.sessionId), []);
assertEquals(await getAreVotedBySessionId([item], undefined), []);
assertEquals(
await getAreVotedBySessionId([item], "not-a-session"),
[],
);
assertEquals(
await getAreVotedBySessionId([item], crypto.randomUUID()),
[],
);

await createItem(item);

await createUser(user);
await createVote(vote);

assertEquals(await getItem(item.id), item);
assertEquals(await getUserBySession(user.sessionId), user);

assertEquals(await getAreVotedBySessionId([item], user.sessionId), [
true,
]);
});

0 comments on commit 668897a

Please sign in to comment.