chore(plugin-redirects): scaffolds tests (#4552)

This commit is contained in:
Jacob Fletcher
2023-12-18 14:47:11 -05:00
committed by GitHub
parent b1f3582764
commit adbd76375f
17 changed files with 237 additions and 224 deletions

View File

@@ -1,2 +0,0 @@
MONGODB_URI=mongodb://localhost/payload-plugin-redirects
PAYLOAD_SECRET=kjnsaldkjfnasdljkfghbnseanljnuadlrigjandrg

View File

@@ -1,4 +0,0 @@
{
"ext": "ts",
"exec": "ts-node src/server.ts"
}

View File

@@ -1,28 +0,0 @@
{
"name": "payload-starter-typescript",
"description": "Blank template - no collections",
"version": "1.0.0",
"main": "dist/server.js",
"license": "MIT",
"scripts": {
"dev": "cross-env PAYLOAD_SEED=true PAYLOAD_DROP_DATABASE=true PAYLOAD_CONFIG_PATH=src/payload.config.ts nodemon",
"build:payload": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload build",
"build:server": "tsc",
"build": "yarn build:payload && yarn build:server",
"serve": "cross-env PAYLOAD_CONFIG_PATH=dist/payload.config.js NODE_ENV=production ts-node server.js",
"generate:types": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:types"
},
"dependencies": {
"@swc/core": "^1.3.32",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"payload": "^1.8.2"
},
"devDependencies": {
"@types/express": "^4.17.9",
"cross-env": "^7.0.3",
"nodemon": "^2.0.6",
"ts-node": "^10.9.1",
"typescript": "^4.1.3"
}
}

View File

@@ -1,29 +0,0 @@
import type { CollectionConfig } from 'payload/types'
const Pages: CollectionConfig = {
slug: 'pages',
admin: {
useAsTitle: 'title',
},
fields: [
{
name: 'title',
type: 'text',
required: true,
},
{
name: 'excerpt',
type: 'text',
},
{
name: 'slug',
type: 'text',
required: true,
admin: {
position: 'sidebar',
},
},
],
}
export default Pages

View File

@@ -1,45 +0,0 @@
import path from 'path'
import { buildConfig } from 'payload/config'
import redirects from '../../src'
import Pages from './collections/Pages'
// to build this demo you must import the plugin from dist
// import redirects from '../../dist';
import Users from './collections/Users'
export default buildConfig({
serverURL: 'http://localhost:3000',
admin: {
user: Users.slug,
webpack: config => {
const newConfig = {
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
react: path.join(__dirname, '../node_modules/react'),
'react-dom': path.join(__dirname, '../node_modules/react-dom'),
payload: path.join(__dirname, '../node_modules/payload'),
},
},
}
return newConfig
},
},
collections: [Users, Pages],
localization: {
locales: ['en', 'es'],
defaultLocale: 'en',
fallback: true,
},
plugins: [
redirects({
collections: ['pages'],
}),
],
typescript: {
outputFile: path.resolve(__dirname, 'payload-types.ts'),
},
})

View File

@@ -1,47 +0,0 @@
import type { Payload } from 'payload'
export const seed = async (payload: Payload): Promise<void> => {
payload.logger.info('Seeding data...')
await payload.create({
collection: 'users',
data: {
email: 'dev@payloadcms.com',
password: 'test',
},
})
const { id: homePageID } = await payload.create({
collection: 'pages',
data: {
title: 'Home Page',
slug: 'home',
excerpt: 'This is the home page',
},
})
await payload.create({
collection: 'redirects',
data: {
from: 'https://payloadcms.com/old',
to: {
type: 'reference',
reference: {
relationTo: 'pages',
value: homePageID,
},
},
},
})
await payload.create({
collection: 'redirects',
data: {
from: 'https://payloadcms.com/bad',
to: {
type: 'custom',
url: 'https://payloadcms.com/good',
},
},
})
}

View File

@@ -1,34 +0,0 @@
import dotenv from 'dotenv'
dotenv.config()
import express from 'express'
import payload from 'payload'
import { seed } from './seed'
const app = express()
// Redirect root to Admin panel
app.get('/', (_, res) => {
res.redirect('/admin')
})
// Initialize Payload
const start = async (): Promise<void> => {
await payload.init({
secret: process.env.PAYLOAD_SECRET,
mongoURL: process.env.MONGODB_URI,
express: app,
onInit: () => {
payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`)
},
})
if (process.env.PAYLOAD_SEED === 'true') {
await seed(payload)
}
app.listen(3000)
}
start()

View File

@@ -1,29 +0,0 @@
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"strict": false,
"esModuleInterop": true,
"skipLibCheck": true,
"outDir": "./dist",
"rootDir": "../",
"jsx": "react"
},
"include": [
"src"
],
"exclude": [
"node_modules",
"dist",
"build",
],
"ts-node": {
"swc": true
}
}

View File

@@ -0,0 +1,30 @@
import type { CollectionConfig } from 'payload/dist/collections/config/types'
import { pagesSlug } from '../shared'
export const Pages: CollectionConfig = {
slug: pagesSlug,
labels: {
singular: 'Page',
plural: 'Pages',
},
admin: {
useAsTitle: 'title',
},
versions: {
drafts: true,
},
fields: [
{
name: 'title',
label: 'Title',
type: 'text',
required: true,
},
{
name: 'excerpt',
label: 'Excerpt',
type: 'text',
},
],
}

View File

@@ -1,6 +1,6 @@
import type { CollectionConfig } from 'payload/types' import type { CollectionConfig } from 'payload/dist/collections/config/types'
const Users: CollectionConfig = { export const Users: CollectionConfig = {
slug: 'users', slug: 'users',
auth: true, auth: true,
admin: { admin: {
@@ -14,5 +14,3 @@ const Users: CollectionConfig = {
// Add more fields as needed // Add more fields as needed
], ],
} }
export default Users

View File

@@ -0,0 +1,31 @@
import redirectsPlugin from '../../packages/plugin-redirects/src'
import { buildConfigWithDefaults } from '../buildConfigWithDefaults'
import { devUser } from '../credentials'
import { Pages } from './collections/Pages'
import { Users } from './collections/Users'
import { seed } from './seed'
export default buildConfigWithDefaults({
collections: [Users, Pages],
localization: {
defaultLocale: 'en',
fallback: true,
locales: ['en', 'es', 'de'],
},
onInit: async (payload) => {
await payload.create({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
},
})
await seed(payload)
},
plugins: [
redirectsPlugin({
collections: ['pages'],
}),
],
})

View File

@@ -0,0 +1,65 @@
import payload from '../../packages/payload/src'
import { initPayloadTest } from '../helpers/configHelpers'
import { pagesSlug } from './shared'
describe('Redirects Plugin', () => {
let page: Page
beforeAll(async () => {
await initPayloadTest({ __dirname, init: { local: true } })
page = await payload.create({
collection: 'pages',
data: {
title: 'Test',
},
})
})
it('should add a redirects collection', async () => {
const redirect = await payload.find({
collection: 'redirects',
depth: 0,
limit: 1,
})
expect(redirect).toBeTruthy()
})
it('should add a redirect with to internal page', async () => {
const redirect = await payload.create({
collection: 'redirects',
data: {
from: '/test',
to: {
type: 'reference',
reference: {
relationTo: pagesSlug,
value: page.id,
},
},
},
})
expect(redirect).toBeTruthy()
expect(redirect.from).toBe('/test')
expect(redirect.to.reference.value).toMatchObject(page)
})
it('should add a redirect with to custom url', async () => {
const redirect = await payload.create({
collection: 'redirects',
data: {
from: '/test2',
to: {
type: 'custom',
url: '/test',
},
},
})
expect(redirect).toBeTruthy()
expect(redirect.from).toBe('/test2')
expect(redirect.to.url).toBe('/test')
})
})

View File

@@ -0,0 +1,83 @@
/* tslint:disable */
/* eslint-disable */
/**
* This file was automatically generated by Payload.
* DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,
* and re-run `payload generate:types` to regenerate this file.
*/
export interface Config {
collections: {
users: User
pages: Page
redirects: Redirect
'payload-preferences': PayloadPreference
'payload-migrations': PayloadMigration
}
globals: {}
}
export interface User {
id: string
updatedAt: string
createdAt: string
email: string
resetPasswordToken?: string | null
resetPasswordExpiration?: string | null
salt?: string | null
hash?: string | null
loginAttempts?: number | null
lockUntil?: string | null
password: string | null
}
export interface Page {
id: string
title: string
excerpt?: string | null
updatedAt: string
createdAt: string
_status?: ('draft' | 'published') | null
}
export interface Redirect {
id: string
from: string
to?: {
type?: ('reference' | 'custom') | null
reference?: {
relationTo: 'pages'
value: string | Page
} | null
url?: string | null
}
updatedAt: string
createdAt: string
}
export interface PayloadPreference {
id: string
user: {
relationTo: 'users'
value: string | User
}
key?: string | null
value?:
| {
[k: string]: unknown
}
| unknown[]
| string
| number
| boolean
| null
updatedAt: string
createdAt: string
}
export interface PayloadMigration {
id: string
name?: string | null
batch?: number | null
updatedAt: string
createdAt: string
}
declare module 'payload' {
export interface GeneratedTypes extends Config {}
}

View File

@@ -0,0 +1,23 @@
import type { Payload } from '../../../packages/payload/src'
import type { PayloadRequest } from '../../../packages/payload/types'
export const seed = async (payload: Payload): Promise<boolean> => {
payload.logger.info('Seeding data...')
const req = {} as PayloadRequest
try {
await payload.create({
collection: 'users',
data: {
email: 'demo@payloadcms.com',
password: 'demo',
},
req,
})
return true
} catch (err) {
console.error(err)
return false
}
}

View File

@@ -0,0 +1 @@
export const pagesSlug = 'pages'

View File

@@ -1,6 +1,6 @@
import type { CollectionConfig } from '../../../packages/payload/src/collections/config/types' import type { CollectionConfig } from '../../../packages/payload/src/collections/config/types'
import { postsSlug } from '../../_community/collections/Posts' import { postsSlug } from '../shared'
export const Posts: CollectionConfig = { export const Posts: CollectionConfig = {
slug: postsSlug, slug: postsSlug,

View File

@@ -1,3 +1,3 @@
export const pagesSlug = 'pages' export const pagesSlug = 'pages'
export const productsSlug = 'posts' export const postsSlug = 'posts'