Fix linting.

This commit is contained in:
Bauke 2023-04-10 11:43:36 +02:00
parent c853802b68
commit 544d915233
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 31 additions and 31 deletions

View File

@ -1,11 +1,11 @@
import {type TestContext, setup} from '@holllo/test'; import {type TestContext, setup} from "@holllo/test";
import {type Value} from '@holllo/webextension-storage'; import {type Value} from "@holllo/webextension-storage";
import browser from 'webextension-polyfill'; import browser from "webextension-polyfill";
import {type Item, createItem, nextItem, nextItemId, storage} from './item.js'; import {type Item, createItem, nextItem, nextItemId, storage} from "./item.js";
const testText = 'Test Item'; const testText = "Test Item";
const testUrl = 'https://example.org/'; const testUrl = "https://example.org/";
/** /**
* Check all properties of an {@link Item}. * Check all properties of an {@link Item}.
@ -15,19 +15,19 @@ const testUrl = 'https://example.org/';
*/ */
function assertItem(item: Value<Item>, test: TestContext): void { function assertItem(item: Value<Item>, test: TestContext): void {
// Assert that itemKeyPrefix is used. // Assert that itemKeyPrefix is used.
test.true(/^item-\d+$/.test(item.key), 'item key regex'); test.true(/^item-\d+$/.test(item.key), "item key regex");
// Assert that deserialization instantiates any classes. // Assert that deserialization instantiates any classes.
test.true(item.value.dateAdded instanceof Date, 'dateAdded is a Date'); test.true(item.value.dateAdded instanceof Date, "dateAdded is a Date");
// Assert that the expected values are indeed present. // Assert that the expected values are indeed present.
test.true(item.value.id > 0, 'id is set'); test.true(item.value.id > 0, "id is set");
test.equals(item.value.text, testText, 'text is set'); test.equals(item.value.text, testText, "text is set");
test.equals(item.value.url, testUrl, 'url is set'); test.equals(item.value.url, testUrl, "url is set");
} }
await setup( await setup(
'Item', "Item",
async (group) => { async (group) => {
group.beforeAll(async () => { group.beforeAll(async () => {
// If we're in production and testing, clear item storage. // If we're in production and testing, clear item storage.
@ -36,7 +36,7 @@ await setup(
} }
}); });
group.test('create & nextItem', async (test) => { group.test("create & nextItem", async (test) => {
const testItem = await createItem(testText, testUrl); const testItem = await createItem(testText, testUrl);
assertItem(testItem, test); assertItem(testItem, test);
await testItem.save(); await testItem.save();
@ -44,11 +44,11 @@ await setup(
// Make sure `nextItem()` returns an item. // Make sure `nextItem()` returns an item.
let storedNext = await nextItem(); let storedNext = await nextItem();
if (storedNext === undefined) { if (storedNext === undefined) {
throw new Error('Expected an item'); throw new Error("Expected an item");
} }
// Assert that our first test item and the stored one are identical. // Assert that our first test item and the stored one are identical.
test.equals(storedNext.key, testItem.key, 'id check'); test.equals(storedNext.key, testItem.key, "id check");
assertItem(storedNext, test); assertItem(storedNext, test);
// Store all test items we create so we can remove them later on. // Store all test items we create so we can remove them later on.
@ -57,7 +57,7 @@ await setup(
// Create a bunch of test items and assert them all. // Create a bunch of test items and assert them all.
for (let index = 1; index < 10; index++) { for (let index = 1; index < 10; index++) {
const next = await createItem(testText, testUrl); const next = await createItem(testText, testUrl);
test.equals(testItem.value.id + index, next.value.id, 'id check'); test.equals(testItem.value.id + index, next.value.id, "id check");
assertItem(next, test); assertItem(next, test);
items.push(next); items.push(next);
await next.save(); await next.save();
@ -73,19 +73,19 @@ await setup(
// TODO: Temporarily store existing storage and run the tests, and then // TODO: Temporarily store existing storage and run the tests, and then
// restore it again. // restore it again.
storedNext = await nextItem(); storedNext = await nextItem();
test.equals(storedNext, undefined, 'next item is undefined'); test.equals(storedNext, undefined, "next item is undefined");
}); });
group.test('nextItemId', async (test) => { group.test("nextItemId", async (test) => {
const testItem = await createItem(testText, testUrl); const testItem = await createItem(testText, testUrl);
assertItem(testItem, test); assertItem(testItem, test);
await testItem.save(); await testItem.save();
const id = await nextItemId(); const id = await nextItemId();
test.equals(typeof id, 'number', 'id is a number'); test.equals(typeof id, "number", "id is a number");
test.false(Number.isNaN(id), 'id is not NaN'); test.false(Number.isNaN(id), "id is not NaN");
test.true(id > 0, 'id larger than 0'); test.true(id > 0, "id larger than 0");
test.equals(await nextItemId(), testItem.value.id + 1, 'id check'); test.equals(await nextItemId(), testItem.value.id + 1, "id check");
await testItem.remove(); await testItem.remove();
}); });
}, },

View File

@ -1,5 +1,5 @@
import browser from 'webextension-polyfill'; import browser from "webextension-polyfill";
import {createValue, type Value} from '@holllo/webextension-storage'; import {createValue, type Value} from "@holllo/webextension-storage";
/** A queued item. */ /** A queued item. */
export type Item = { export type Item = {
@ -29,7 +29,7 @@ export type SerializedItem = {
}; };
/** The key prefix for {@link Item}s. */ /** The key prefix for {@link Item}s. */
export const itemKeyPrefix = 'item-'; export const itemKeyPrefix = "item-";
/** The default storage area to use for {@link Item}s. */ /** The default storage area to use for {@link Item}s. */
export const storage = browser.storage.sync; export const storage = browser.storage.sync;
@ -40,13 +40,13 @@ export const storage = browser.storage.sync;
* @param input The {@link Item} to serialize. * @param input The {@link Item} to serialize.
* @returns The serialized {@link Item} string. * @returns The serialized {@link Item} string.
*/ */
export const serializeItem: Value<Item>['serialize'] = ( export const serializeItem: Value<Item>["serialize"] = (
input: Item, input: Item,
): string => { ): string => {
const serialized: SerializedItem = { const serialized: SerializedItem = {
dateAdded: input.dateAdded.toISOString(), dateAdded: input.dateAdded.toISOString(),
id: input.id.toString(), id: input.id.toString(),
text: input.text ?? '', text: input.text ?? "",
url: input.url, url: input.url,
}; };
@ -63,7 +63,7 @@ export const serializeItem: Value<Item>['serialize'] = (
* @param input The {@link Item} string to deserialize. * @param input The {@link Item} string to deserialize.
* @returns The deserialized {@link Item}. * @returns The deserialized {@link Item}.
*/ */
export const deserializeItem: Value<Item>['deserialize'] = ( export const deserializeItem: Value<Item>["deserialize"] = (
input: string, input: string,
): Item => { ): Item => {
const parsed = JSON.parse(input) as SerializedItem; const parsed = JSON.parse(input) as SerializedItem;
@ -73,7 +73,7 @@ export const deserializeItem: Value<Item>['deserialize'] = (
id: Number(parsed.id), id: Number(parsed.id),
// In `serializeItem()` the item text is set to an empty string when // In `serializeItem()` the item text is set to an empty string when
// undefined, so revert it back to undefined here if that's the case. // undefined, so revert it back to undefined here if that's the case.
text: parsed.text === '' ? undefined : parsed.text, text: parsed.text === "" ? undefined : parsed.text,
url: parsed.url, url: parsed.url,
}; };
}; };
@ -86,8 +86,8 @@ export const deserializeItem: Value<Item>['deserialize'] = (
* @returns The created {@link Value} with inner {@link Item}. * @returns The created {@link Value} with inner {@link Item}.
*/ */
export async function createItem( export async function createItem(
text: Item['text'], text: Item["text"],
url: Item['url'], url: Item["url"],
): Promise<Value<Item>> { ): Promise<Value<Item>> {
const nextId = await nextItemId(); const nextId = await nextItemId();