From 1ded74f5379564df24eb0cbb7fec9cc580fbfbf1 Mon Sep 17 00:00:00 2001 From: Mihovil Ilakovac Date: Wed, 31 Jan 2024 12:31:52 +0100 Subject: [PATCH] Implement wasp/client/test API (#1704) --- .../Generator/templates/sdk/{ => client}/test/index.ts | 0 .../templates/sdk/{ => client}/test/vitest/helpers.tsx | 5 +++++ waspc/data/Generator/templates/sdk/package.json | 7 +++---- waspc/examples/todo-typescript/src/Todo.test.tsx | 2 +- waspc/src/Wasp/Generator/SdkGenerator.hs | 4 ++-- 5 files changed, 11 insertions(+), 7 deletions(-) rename waspc/data/Generator/templates/sdk/{ => client}/test/index.ts (100%) rename waspc/data/Generator/templates/sdk/{ => client}/test/vitest/helpers.tsx (97%) diff --git a/waspc/data/Generator/templates/sdk/test/index.ts b/waspc/data/Generator/templates/sdk/client/test/index.ts similarity index 100% rename from waspc/data/Generator/templates/sdk/test/index.ts rename to waspc/data/Generator/templates/sdk/client/test/index.ts diff --git a/waspc/data/Generator/templates/sdk/test/vitest/helpers.tsx b/waspc/data/Generator/templates/sdk/client/test/vitest/helpers.tsx similarity index 97% rename from waspc/data/Generator/templates/sdk/test/vitest/helpers.tsx rename to waspc/data/Generator/templates/sdk/client/test/vitest/helpers.tsx index 6c4eadbec3..aa2eba4fa8 100644 --- a/waspc/data/Generator/templates/sdk/test/vitest/helpers.tsx +++ b/waspc/data/Generator/templates/sdk/client/test/vitest/helpers.tsx @@ -10,15 +10,19 @@ import { Query } from 'wasp/rpc' import config from 'wasp/core/config' import { HttpMethod, Route } from 'wasp/types' +// PRIVATE API export type { Route } from 'wasp/types' +// PRIVATE API export type MockQuery = ( query: Query, resJson: MockOutput ) => void +// PRIVATE API export type MockApi = (route: Route, resJson: unknown) => void +// PUBLIC API // Inspired by the Tanstack React Query helper: // https://github.com/TanStack/query/blob/4ae99561ca3383d6de3f4aad656a49ba4a17b57a/packages/react-query/src/__tests__/utils.tsx#L7-L26 export function renderInContext(ui: ReactElement): RenderResult { @@ -39,6 +43,7 @@ export function renderInContext(ui: ReactElement): RenderResult { } } +// PUBLIC API export function mockServer(): { server: SetupServer mockQuery: MockQuery diff --git a/waspc/data/Generator/templates/sdk/package.json b/waspc/data/Generator/templates/sdk/package.json index b3f7feeea7..e853eed44a 100644 --- a/waspc/data/Generator/templates/sdk/package.json +++ b/waspc/data/Generator/templates/sdk/package.json @@ -74,10 +74,8 @@ "./server/queries": "./dist/server/queries/index.js", {=! Used by our code, uncodumented (but accessible) for users. =} "./server/auth/email": "./dist/server/auth/email/index.js", - {=! Used by users, documented. =} - "./test": "./dist/test/index.js", {=! Used by our code, uncodumented (but accessible) for users. =} - "./test/*": "./dist/test/*.js", + "./client/test/*": "./dist/client/test/*.js", {=! Used by our code, uncodumented (but accessible) for users. =} "./server/crud/*": "./dist/server/crud/*", {=! Used by our code, uncodumented (but accessible) for users. =} @@ -118,7 +116,8 @@ "./client/crud": "./dist/client/crud/index.js", "./server/email": "./dist/server/email/index.js", "./server/jobs": "./dist/server/jobs/index.js", - "./client/router": "./dist/client/router/index.js" + "./client/router": "./dist/client/router/index.js", + "./client/test": "./dist/client/test/index.js" }, {=! TypeScript doesn't care about the redirects we define above in "exports" field; those diff --git a/waspc/examples/todo-typescript/src/Todo.test.tsx b/waspc/examples/todo-typescript/src/Todo.test.tsx index ce54febd76..87bff2532d 100644 --- a/waspc/examples/todo-typescript/src/Todo.test.tsx +++ b/waspc/examples/todo-typescript/src/Todo.test.tsx @@ -1,7 +1,7 @@ import { test, expect } from 'vitest' import { screen } from '@testing-library/react' -import { mockServer, renderInContext } from 'wasp/test' +import { mockServer, renderInContext } from 'wasp/client/test' import { getTasks } from 'wasp/rpc/queries' import { Todo, areThereAnyTasks } from './Todo' import { MainPage } from './MainPage' diff --git a/waspc/src/Wasp/Generator/SdkGenerator.hs b/waspc/src/Wasp/Generator/SdkGenerator.hs index d5221ab229..719f89851d 100644 --- a/waspc/src/Wasp/Generator/SdkGenerator.hs +++ b/waspc/src/Wasp/Generator/SdkGenerator.hs @@ -93,9 +93,9 @@ genSdkReal spec = genFileCopy [relfile|server/HttpError.ts|], genFileCopy [relfile|server/AuthError.ts|], genFileCopy [relfile|types/index.ts|], - genFileCopy [relfile|test/vitest/helpers.tsx|], - genFileCopy [relfile|test/index.ts|], genFileCopy [relfile|server/jobs/pgBoss/types.ts|], + genFileCopy [relfile|client/test/vitest/helpers.tsx|], + genFileCopy [relfile|client/test/index.ts|], genServerConfigFile spec, genTsConfigJson, genServerUtils spec,