Page MenuHomeSealhub

photo.test.ts
No OneTemporary

photo.test.ts

import Router from "@koa/router";
import getPort from "get-port";
import { Browser, Page } from "playwright";
import type { Context } from "koa";
import Koa from "koa";
import { mount } from "../../mount.js";
import { Form } from "../form.js";
import { getBrowser } from "../../utils/browser-creator.js";
import { Fields } from "../../index.js";
import { Photo } from "./photo.js";
import { KoaResponsiveImageRouter } from "koa-responsive-image-router";
import _locreq from "locreq";
import { module_dirname } from "../../utils/module-dirname.js";
import { FileManager, PathFilePointer } from "sealious";
const locreq = _locreq(module_dirname(import.meta.url));
describe("photo-controll", () => {
describe("basic tests", async () => {
let server: ReturnType<Koa["listen"]>;
let browser: Browser;
let page: Page;
afterAll(() => {
server.close();
});
it("sends the old photo if it wasn't changed and it has an initial value", async () => {
const port = await getPort();
console.log(`Using port ${port} for photo.test.ts`);
const app = new Koa();
const router = new Router();
const fields = {
photo: new Fields.File(false, new FileManager("/tmp")),
};
let received_photo: unknown;
const imageRouter = new KoaResponsiveImageRouter({
staticPath: "/images",
thumbnailSize: 20,
cacheManagerResolutionThreshold: 50,
imageStoragePath: `/tmp/image-cache-${port}`,
smartCropStoragePath: `/tmp/smartcrop-storage-${port}`,
});
mount(
router,
"/",
new (class extends Form<typeof fields, unknown> {
fields = fields;
controls = [new Photo(fields.photo, imageRouter)];
async onSubmit(ctx: Context) {
console.log("OnSubmit!");
const data = await this.getParsedValues(ctx);
console.log({ data });
received_photo = data.photo;
}
async getInitialValues(ctx: Context) {
const file = new PathFilePointer(
new FileManager("/tmp"),
locreq.resolve("assets/important.png")
);
return {
photo: await file.save(false), // to get the token
};
}
async canAccess(_: Context) {
return { canAccess: true, message: "" };
}
})(),
true
);
app.use(router.routes()).use(router.allowedMethods());
server = app.listen(port);
browser = await getBrowser();
const context = await browser.newContext();
page = await context.newPage();
await page.goto(`http://localhost:${port}`);
await page.getByRole("button", { name: "Wyślij" }).click();
console.log({ received_photo });
assert(received_photo !== undefined && (received_photo as any).old !== null);
});
});
});

File Metadata

Mime Type
text/x-java
Expires
Tue, Dec 24, 14:02 (17 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557205
Default Alt Text
photo.test.ts (2 KB)

Event Timeline