chore(examples/testing): builds testing example (#3443)
This commit is contained in:
35
examples/testing/src/payload-types.ts
Normal file
35
examples/testing/src/payload-types.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/* 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: {
|
||||
posts: Post
|
||||
users: User
|
||||
}
|
||||
globals: {}
|
||||
}
|
||||
export interface Post {
|
||||
id: string
|
||||
title?: string
|
||||
author?: string | User
|
||||
updatedAt: string
|
||||
createdAt: string
|
||||
}
|
||||
export interface User {
|
||||
id: string
|
||||
updatedAt: string
|
||||
createdAt: string
|
||||
email: string
|
||||
resetPasswordToken?: string
|
||||
resetPasswordExpiration?: string
|
||||
salt?: string
|
||||
hash?: string
|
||||
loginAttempts?: number
|
||||
lockUntil?: string
|
||||
password?: string
|
||||
}
|
||||
33
examples/testing/src/payload.config.ts
Normal file
33
examples/testing/src/payload.config.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import path from 'path'
|
||||
import dotenv from 'dotenv'
|
||||
import { buildConfig } from 'payload/config'
|
||||
|
||||
dotenv.config({
|
||||
path: path.resolve(__dirname, '../.env'),
|
||||
})
|
||||
|
||||
export default buildConfig({
|
||||
serverURL: process.env.PAYLOAD_PUBLIC_SERVER_URL,
|
||||
typescript: {
|
||||
outputFile: path.resolve(__dirname, 'payload-types.ts'),
|
||||
},
|
||||
collections: [
|
||||
{
|
||||
slug: 'posts',
|
||||
admin: {
|
||||
useAsTitle: 'title',
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
type: 'text',
|
||||
},
|
||||
{
|
||||
name: 'author',
|
||||
type: 'relationship',
|
||||
relationTo: 'users',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
})
|
||||
24
examples/testing/src/server.ts
Normal file
24
examples/testing/src/server.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import path from 'path'
|
||||
import express from 'express'
|
||||
import payload from 'payload'
|
||||
|
||||
// Use `dotenv` to import your `.env` file automatically
|
||||
require('dotenv').config({
|
||||
path: path.resolve(__dirname, '../.env'),
|
||||
})
|
||||
|
||||
const app = express()
|
||||
|
||||
async function start() {
|
||||
await payload.init({
|
||||
secret: process.env.PAYLOAD_SECRET_KEY,
|
||||
mongoURL: process.env.MONGO_URL,
|
||||
express: app,
|
||||
})
|
||||
|
||||
app.listen(process.env.PORT, async () => {
|
||||
console.log(`Express is now listening for incoming connections on port ${process.env.PORT}.`)
|
||||
})
|
||||
}
|
||||
|
||||
start()
|
||||
4
examples/testing/src/tests/credentials.ts
Normal file
4
examples/testing/src/tests/credentials.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export default {
|
||||
email: 'test@test.com',
|
||||
password: 'test',
|
||||
}
|
||||
44
examples/testing/src/tests/globalSetup.ts
Normal file
44
examples/testing/src/tests/globalSetup.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { resolve } from 'path'
|
||||
import payload from 'payload'
|
||||
import express from 'express'
|
||||
import testCredentials from './credentials'
|
||||
|
||||
require('dotenv').config({
|
||||
path: resolve(__dirname, '../../.env'),
|
||||
})
|
||||
|
||||
const app = express()
|
||||
|
||||
const globalSetup = async () => {
|
||||
await payload.init({
|
||||
secret: process.env.PAYLOAD_SECRET_KEY,
|
||||
mongoURL: process.env.MONGO_URL,
|
||||
express: app,
|
||||
})
|
||||
|
||||
app.listen(process.env.PORT, async () => {
|
||||
console.log(`Express is now listening for incoming connections on port ${process.env.PORT}.`)
|
||||
})
|
||||
|
||||
const response = await fetch(
|
||||
`${process.env.PAYLOAD_PUBLIC_SERVER_URL}/api/users/first-register`,
|
||||
{
|
||||
body: JSON.stringify({
|
||||
email: testCredentials.email,
|
||||
password: testCredentials.password,
|
||||
}),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
method: 'post',
|
||||
},
|
||||
)
|
||||
|
||||
const data = await response.json()
|
||||
|
||||
if (!data.user || !data.user.token) {
|
||||
throw new Error('Failed to register first user')
|
||||
}
|
||||
}
|
||||
|
||||
export default globalSetup
|
||||
22
examples/testing/src/tests/login.spec.ts
Normal file
22
examples/testing/src/tests/login.spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { User } from '../payload-types'
|
||||
import testCredentials from './credentials'
|
||||
|
||||
describe('Users', () => {
|
||||
it('should allow a user to log in', async () => {
|
||||
const result: {
|
||||
token: string
|
||||
user: User
|
||||
} = await fetch(`${process.env.PAYLOAD_PUBLIC_SERVER_URL}/api/users/login`, {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
email: testCredentials.email,
|
||||
password: testCredentials.password,
|
||||
}),
|
||||
}).then((res) => res.json())
|
||||
|
||||
expect(result.token).toBeDefined()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user