mirror of
https://github.com/zenstackhq/zenstack
synced 2026-05-24 10:08:55 +00:00
Co-authored-by: Augustin <43639468+Azzerty23@users.noreply.github.com> Co-authored-by: Jonathan Stevens <jonathan.stevens@resnovas.com> Co-authored-by: Jonathan S <jonathan.stevens@eventiva.co.uk> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: ErikMCM <70036542+ErikMCM@users.noreply.github.com> Co-authored-by: Jason Kleinberg <ustice@gmail.com> Co-authored-by: Jonathan S <punk.gift9475@alias.org.uk> Co-authored-by: Jiasheng <jiashengguo@outlook.com>
236 lines
6.2 KiB
TypeScript
236 lines
6.2 KiB
TypeScript
/// <reference types="@types/jest" />
|
|
|
|
import { loadSchema, normalizePath } from '@zenstackhq/testtools';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import tmp from 'tmp';
|
|
|
|
describe('Tanstack Query Plugin Tests', () => {
|
|
let origDir: string;
|
|
|
|
beforeAll(() => {
|
|
origDir = process.cwd();
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.chdir(origDir);
|
|
});
|
|
|
|
const sharedModel = `
|
|
model User {
|
|
id String @id
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
email String @unique
|
|
role role @default(USER)
|
|
posts post_Item[]
|
|
}
|
|
|
|
enum role {
|
|
USER
|
|
ADMIN
|
|
}
|
|
|
|
model post_Item {
|
|
id String @id
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
title String
|
|
author User? @relation(fields: [authorId], references: [id], onDelete: Cascade)
|
|
authorId String?
|
|
published Boolean @default(false)
|
|
viewCount Int @default(0)
|
|
}
|
|
|
|
model Foo {
|
|
id String @id
|
|
@@ignore
|
|
}
|
|
`;
|
|
|
|
it('react-query run plugin v4', async () => {
|
|
await loadSchema(
|
|
`
|
|
plugin tanstack {
|
|
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
|
|
output = '$projectRoot/hooks'
|
|
target = 'react'
|
|
version = 'v4'
|
|
}
|
|
|
|
${sharedModel}
|
|
`,
|
|
{
|
|
provider: 'postgresql',
|
|
pushDb: false,
|
|
extraDependencies: ['react@18.2.0', '@types/react@18.2.0', '@tanstack/react-query@4.29.7'],
|
|
copyDependencies: [path.resolve(__dirname, '../dist')],
|
|
compile: true,
|
|
}
|
|
);
|
|
});
|
|
|
|
it('react-query run plugin v5', async () => {
|
|
await loadSchema(
|
|
`
|
|
plugin tanstack {
|
|
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
|
|
output = '$projectRoot/hooks'
|
|
target = 'react'
|
|
}
|
|
|
|
${sharedModel}
|
|
`,
|
|
{
|
|
provider: 'postgresql',
|
|
pushDb: false,
|
|
extraDependencies: ['react@18.2.0', '@types/react@18.2.0', '@tanstack/react-query@^5.0.0'],
|
|
copyDependencies: [path.resolve(__dirname, '../dist')],
|
|
compile: true,
|
|
}
|
|
);
|
|
});
|
|
|
|
it('vue-query run plugin v4', async () => {
|
|
await loadSchema(
|
|
`
|
|
plugin tanstack {
|
|
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
|
|
output = '$projectRoot/hooks'
|
|
target = 'vue'
|
|
version = 'v4'
|
|
}
|
|
|
|
${sharedModel}
|
|
`,
|
|
{
|
|
provider: 'postgresql',
|
|
pushDb: false,
|
|
extraDependencies: ['vue@^3.3.4', '@tanstack/vue-query@4.37.0'],
|
|
copyDependencies: [path.resolve(__dirname, '../dist')],
|
|
compile: true,
|
|
}
|
|
);
|
|
});
|
|
|
|
it('vue-query run plugin v5', async () => {
|
|
await loadSchema(
|
|
`
|
|
plugin tanstack {
|
|
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
|
|
output = '$projectRoot/hooks'
|
|
target = 'vue'
|
|
}
|
|
|
|
${sharedModel}
|
|
`,
|
|
{
|
|
provider: 'postgresql',
|
|
pushDb: false,
|
|
extraDependencies: ['vue@^3.3.4', '@tanstack/vue-query@latest'],
|
|
copyDependencies: [path.resolve(__dirname, '../dist')],
|
|
compile: true,
|
|
}
|
|
);
|
|
});
|
|
|
|
it('svelte-query run plugin v4', async () => {
|
|
await loadSchema(
|
|
`
|
|
plugin tanstack {
|
|
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
|
|
output = '$projectRoot/hooks'
|
|
target = 'svelte'
|
|
version = 'v4'
|
|
}
|
|
|
|
${sharedModel}
|
|
`,
|
|
{
|
|
provider: 'postgresql',
|
|
pushDb: false,
|
|
extraDependencies: ['svelte@^3.0.0', '@tanstack/svelte-query@4.29.7'],
|
|
copyDependencies: [path.resolve(__dirname, '../dist')],
|
|
compile: true,
|
|
}
|
|
);
|
|
});
|
|
|
|
it('svelte-query run plugin v5', async () => {
|
|
await loadSchema(
|
|
`
|
|
plugin tanstack {
|
|
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
|
|
output = '$projectRoot/hooks'
|
|
target = 'svelte'
|
|
}
|
|
|
|
${sharedModel}
|
|
`,
|
|
{
|
|
provider: 'postgresql',
|
|
pushDb: false,
|
|
extraDependencies: ['svelte@^3.0.0', '@tanstack/svelte-query@^5.0.0'],
|
|
copyDependencies: [path.resolve(__dirname, '../dist')],
|
|
compile: true,
|
|
}
|
|
);
|
|
});
|
|
|
|
it('clear output', async () => {
|
|
const { name: projectDir } = tmp.dirSync();
|
|
fs.mkdirSync(path.join(projectDir, 'tanstack'), { recursive: true });
|
|
fs.writeFileSync(path.join(projectDir, 'tanstack', 'test.txt'), 'hello');
|
|
|
|
await loadSchema(
|
|
`
|
|
plugin tanstack {
|
|
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
|
|
output = '$projectRoot/tanstack'
|
|
target = 'react'
|
|
}
|
|
|
|
model User {
|
|
id Int @id @default(autoincrement())
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
email String @unique
|
|
password String @omit
|
|
}
|
|
`,
|
|
{
|
|
pushDb: false,
|
|
projectDir,
|
|
extraDependencies: [`${normalizePath(path.join(__dirname, '../dist'))}`],
|
|
}
|
|
);
|
|
|
|
expect(fs.existsSync(path.join(projectDir, 'tanstack', 'test.txt'))).toBeFalsy();
|
|
});
|
|
|
|
it('existing output as file', async () => {
|
|
const { name: projectDir } = tmp.dirSync();
|
|
fs.writeFileSync(path.join(projectDir, 'tanstack'), 'hello');
|
|
|
|
await expect(
|
|
loadSchema(
|
|
`
|
|
plugin tanstack {
|
|
provider = '${normalizePath(path.resolve(__dirname, '../dist'))}'
|
|
output = '$projectRoot/tanstack'
|
|
target = 'react'
|
|
}
|
|
|
|
model User {
|
|
id Int @id @default(autoincrement())
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
email String
|
|
password String @omit
|
|
}
|
|
`,
|
|
{ pushDb: false, projectDir, extraDependencies: [`${normalizePath(path.join(__dirname, '../dist'))}`] }
|
|
)
|
|
).rejects.toThrow('already exists and is not a directory');
|
|
});
|
|
});
|