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 Value} from '@holllo/webextension-storage';
import browser from 'webextension-polyfill';
import {type TestContext, setup} from "@holllo/test";
import {type Value} from "@holllo/webextension-storage";
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 testUrl = 'https://example.org/';
const testText = "Test Item";
const testUrl = "https://example.org/";
/**
* Check all properties of an {@link Item}.
@ -15,19 +15,19 @@ const testUrl = 'https://example.org/';
*/
function assertItem(item: Value<Item>, test: TestContext): void {
// 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.
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.
test.true(item.value.id > 0, 'id is set');
test.equals(item.value.text, testText, 'text is set');
test.equals(item.value.url, testUrl, 'url is set');
test.true(item.value.id > 0, "id is set");
test.equals(item.value.text, testText, "text is set");
test.equals(item.value.url, testUrl, "url is set");
}
await setup(
'Item',
"Item",
async (group) => {
group.beforeAll(async () => {
// 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);
assertItem(testItem, test);
await testItem.save();
@ -44,11 +44,11 @@ await setup(
// Make sure `nextItem()` returns an item.
let storedNext = await nextItem();
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.
test.equals(storedNext.key, testItem.key, 'id check');
test.equals(storedNext.key, testItem.key, "id check");
assertItem(storedNext, test);
// 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.
for (let index = 1; index < 10; index++) {
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);
items.push(next);
await next.save();
@ -73,19 +73,19 @@ await setup(
// TODO: Temporarily store existing storage and run the tests, and then
// restore it again.
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);
assertItem(testItem, test);
await testItem.save();
const id = await nextItemId();
test.equals(typeof id, 'number', 'id is a number');
test.false(Number.isNaN(id), 'id is not NaN');
test.true(id > 0, 'id larger than 0');
test.equals(await nextItemId(), testItem.value.id + 1, 'id check');
test.equals(typeof id, "number", "id is a number");
test.false(Number.isNaN(id), "id is not NaN");
test.true(id > 0, "id larger than 0");
test.equals(await nextItemId(), testItem.value.id + 1, "id check");
await testItem.remove();
});
},

View File

@ -1,5 +1,5 @@
import browser from 'webextension-polyfill';
import {createValue, type Value} from '@holllo/webextension-storage';
import browser from "webextension-polyfill";
import {createValue, type Value} from "@holllo/webextension-storage";
/** A queued item. */
export type Item = {
@ -29,7 +29,7 @@ export type SerializedItem = {
};
/** 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. */
export const storage = browser.storage.sync;
@ -40,13 +40,13 @@ export const storage = browser.storage.sync;
* @param input The {@link Item} to serialize.
* @returns The serialized {@link Item} string.
*/
export const serializeItem: Value<Item>['serialize'] = (
export const serializeItem: Value<Item>["serialize"] = (
input: Item,
): string => {
const serialized: SerializedItem = {
dateAdded: input.dateAdded.toISOString(),
id: input.id.toString(),
text: input.text ?? '',
text: input.text ?? "",
url: input.url,
};
@ -63,7 +63,7 @@ export const serializeItem: Value<Item>['serialize'] = (
* @param input The {@link Item} string to deserialize.
* @returns The deserialized {@link Item}.
*/
export const deserializeItem: Value<Item>['deserialize'] = (
export const deserializeItem: Value<Item>["deserialize"] = (
input: string,
): Item => {
const parsed = JSON.parse(input) as SerializedItem;
@ -73,7 +73,7 @@ export const deserializeItem: Value<Item>['deserialize'] = (
id: Number(parsed.id),
// 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.
text: parsed.text === '' ? undefined : parsed.text,
text: parsed.text === "" ? undefined : parsed.text,
url: parsed.url,
};
};
@ -86,8 +86,8 @@ export const deserializeItem: Value<Item>['deserialize'] = (
* @returns The created {@link Value} with inner {@link Item}.
*/
export async function createItem(
text: Item['text'],
url: Item['url'],
text: Item["text"],
url: Item["url"],
): Promise<Value<Item>> {
const nextId = await nextItemId();