Files
payload/test/uploads/int.spec.ts
Alessio Gravili 90b7b20699 feat!: beta-next (#7620)
This PR makes three major changes to the codebase:

1. [Component Paths](#component-paths)
Instead of importing custom components into your config directly, they
are now defined as file paths and rendered only when needed. That way
the Payload config will be significantly more lightweight, and ensures
that the Payload config is 100% server-only and Node-safe. Related
discussion: https://github.com/payloadcms/payload/discussions/6938

2. [Client Config](#client-config)
Deprecates the component map by merging its logic into the client
config. The main goal of this change is for performance and
simplification. There was no need to deeply iterate over the Payload
config twice, once for the component map, and another for the client
config. Instead, we can do everything in the client config one time.
This has also dramatically simplified the client side prop drilling
through the UI library. Now, all components can share the same client
config which matches the exact shape of their Payload config (with the
exception of non-serializable props and mapped custom components).

3. [Custom client component are no longer
server-rendered](#custom-client-components-are-no-longer-server-rendered)
Previously, custom components would be server-rendered, no matter if
they are server or client components. Now, only server components are
rendered on the server. Client components are automatically detected,
and simply get passed through as `MappedComponent` to be rendered fully
client-side.

## Component Paths

Instead of importing custom components into your config directly, they
are now defined as file paths and rendered only when needed. That way
the Payload config will be significantly more lightweight, and ensures
that the Payload config is 100% server-only and Node-safe. Related
discussion: https://github.com/payloadcms/payload/discussions/6938

In order to reference any custom components in the Payload config, you
now have to specify a string path to the component instead of importing
it.

Old:

```ts
import { MyComponent2} from './MyComponent2.js'

admin: {
  components: {
    Label: MyComponent2
  },
},
```

New:

```ts
admin: {
  components: {
    Label: '/collections/Posts/MyComponent2.js#MyComponent2', // <= has to be a relative path based on a baseDir configured in the Payload config - NOT relative based on the importing file
  },
},
```

### Local API within Next.js routes

Previously, if you used the Payload Local API within Next.js pages, all
the client-side modules are being added to the bundle for that specific
page, even if you only need server-side functionality.

This `/test` route, which uses the Payload local API, was previously 460
kb. It is now down to 91 kb and does not bundle the Payload client-side
admin panel anymore.

All tests done
[here](https://github.com/payloadcms/payload-3.0-demo/tree/feat/path-test)
with beta.67/PR, db-mongodb and default richtext-lexical:

**dev /admin before:**
![CleanShot 2024-07-29 at 22 49
12@2x](https://github.com/user-attachments/assets/4428e766-b368-4bcf-8c18-d0187ab64f3e)

**dev /admin after:**
![CleanShot 2024-07-29 at 22 50
49@2x](https://github.com/user-attachments/assets/f494c848-7247-4b02-a650-a3fab4000de6)

---

**dev /test before:**
![CleanShot 2024-07-29 at 22 56
18@2x](https://github.com/user-attachments/assets/1a7e9500-b859-4761-bf63-abbcdac6f8d6)

**dev /test after:**
![CleanShot 2024-07-29 at 22 47
45@2x](https://github.com/user-attachments/assets/f89aa76d-f2d5-4572-9753-2267f034a45a)

---

**build before:**
![CleanShot 2024-07-29 at 22 57
14@2x](https://github.com/user-attachments/assets/5f8f7281-2a4a-40a5-a788-c30ddcdd51b5)

**build after::**
![CleanShot 2024-07-29 at 22 56
39@2x](https://github.com/user-attachments/assets/ea8772fd-512f-4db0-9a81-4b014715a1b7)

### Usage of the Payload Local API / config outside of Next.js

This will make it a lot easier to use the Payload config / local API in
other, server-side contexts. Previously, you might encounter errors due
to client files (like .scss files) not being allowed to be imported.

## Client Config

Deprecates the component map by merging its logic into the client
config. The main goal of this change is for performance and
simplification. There was no need to deeply iterate over the Payload
config twice, once for the component map, and another for the client
config. Instead, we can do everything in the client config one time.
This has also dramatically simplified the client side prop drilling
through the UI library. Now, all components can share the same client
config which matches the exact shape of their Payload config (with the
exception of non-serializable props and mapped custom components).

This is breaking change. The `useComponentMap` hook no longer exists,
and most component props have changed (for the better):

```ts
const { componentMap } = useComponentMap() // old
const { config } = useConfig() // new
```

The `useConfig` hook has also changed in shape, `config` is now a
property _within_ the context obj:

```ts
const config = useConfig() // old
const { config } = useConfig() // new
```

## Custom Client Components are no longer server rendered

Previously, custom components would be server-rendered, no matter if
they are server or client components. Now, only server components are
rendered on the server. Client components are automatically detected,
and simply get passed through as `MappedComponent` to be rendered fully
client-side.

The benefit of this change:

Custom client components can now receive props. Previously, the only way
for them to receive dynamic props from a parent client component was to
use hooks, e.g. `useFieldProps()`. Now, we do have the option of passing
in props to the custom components directly, if they are client
components. This will be simpler than having to look for the correct
hook.

This makes rendering them on the client a little bit more complex, as
you now have to check if that component is a server component (=>
already has been rendered) or a client component (=> not rendered yet,
has to be rendered here). However, this added complexity has been
alleviated through the easy-to-use `<RenderMappedComponent />` helper.

This helper now also handles rendering arrays of custom components (e.g.
beforeList, beforeLogin ...), which actually makes rendering custom
components easier in some cases.

## Misc improvements

This PR includes misc, breaking changes. For example, we previously
allowed unions between components and config object for the same
property. E.g. for the custom view property, you were allowed to pass in
a custom component or an object with other properties, alongside a
custom component.

Those union types are now gone. You can now either pass an object, or a
component. The previous `{ View: MyViewComponent}` is now `{ View: {
Component: MyViewComponent} }` or `{ View: { Default: { Component:
MyViewComponent} } }`.

This dramatically simplifies the way we read & process those properties,
especially in buildComponentMap. We can now simply check for the
existence of one specific property, which always has to be a component,
instead of running cursed runtime checks on a shared union property
which could contain a component, but could also contain functions or
objects.

![CleanShot 2024-07-29 at 23 07
07@2x](https://github.com/user-attachments/assets/1e75aa4c-7a4c-419f-9070-216bb7b9a5e5)

![CleanShot 2024-07-29 at 23 09
40@2x](https://github.com/user-attachments/assets/b4c96450-6b7e-496c-a4f7-59126bfd0991)

- [x] I have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

---------

Co-authored-by: PatrikKozak <patrik@payloadcms.com>
Co-authored-by: Paul <paul@payloadcms.com>
Co-authored-by: Paul Popus <paul@nouance.io>
Co-authored-by: Jacob Fletcher <jacobsfletch@gmail.com>
Co-authored-by: James <james@trbl.design>
2024-08-13 12:54:33 -04:00

774 lines
24 KiB
TypeScript

import type { Payload } from 'payload'
import fs from 'fs'
import path from 'path'
import { getFileByPath } from 'payload'
import { fileURLToPath } from 'url'
import { promisify } from 'util'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import type { Enlarge, Media } from './payload-types.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
import { createStreamableFile } from './createStreamableFile.js'
import {
enlargeSlug,
focalNoSizesSlug,
focalOnlySlug,
mediaSlug,
reduceSlug,
relationSlug,
unstoredMediaSlug,
usersSlug,
} from './shared.js'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const stat = promisify(fs.stat)
let restClient: NextRESTClient
let payload: Payload
describe('Collections - Uploads', () => {
beforeAll(async () => {
;({ payload, restClient } = await initPayloadInt(dirname))
await restClient.login({ slug: usersSlug })
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy()
}
})
describe('REST API', () => {
describe('create', () => {
it('creates from form data given a png', async () => {
const formData = new FormData()
const filePath = path.join(dirname, './image.png')
const { file, handle } = await createStreamableFile(filePath)
formData.append('file', file)
const response = await restClient.POST(`/${mediaSlug}`, {
body: formData,
file,
})
const { doc } = await response.json()
await handle.close()
expect(response.status).toBe(201)
const { sizes } = doc
const expectedPath = path.join(dirname, './media')
// Check for files
expect(await fileExists(path.join(expectedPath, doc.filename))).toBe(true)
expect(
await fileExists(path.join(expectedPath, sizes.maintainedAspectRatio.filename)),
).toBe(true)
expect(await fileExists(path.join(expectedPath, sizes.tablet.filename))).toBe(true)
expect(await fileExists(path.join(expectedPath, sizes.mobile.filename))).toBe(true)
expect(await fileExists(path.join(expectedPath, sizes.icon.filename))).toBe(true)
// Check api response
expect(doc.mimeType).toEqual('image/png')
expect(doc.focalX).toEqual(50)
expect(doc.focalY).toEqual(50)
expect(sizes.maintainedAspectRatio.url).toContain('/api/media/file/image')
expect(sizes.maintainedAspectRatio.url).toContain('.png')
expect(sizes.maintainedAspectRatio.width).toEqual(1024)
expect(sizes.maintainedAspectRatio.height).toEqual(1024)
expect(sizes).toHaveProperty('tablet')
expect(sizes).toHaveProperty('mobile')
expect(sizes).toHaveProperty('icon')
})
it('creates from form data given an svg', async () => {
const filePath = path.join(dirname, './image.svg')
const formData = new FormData()
const { file, handle } = await createStreamableFile(filePath)
formData.append('file', file)
const response = await restClient.POST(`/${mediaSlug}`, {
body: formData,
file,
})
const { doc } = await response.json()
await handle.close()
expect(response.status).toBe(201)
// Check for files
expect(await fileExists(path.join(dirname, './media', doc.filename))).toBe(true)
// Check api response
expect(doc.mimeType).toEqual('image/svg+xml')
expect(doc.sizes.maintainedAspectRatio.url).toBeFalsy()
expect(doc.width).toBeDefined()
expect(doc.height).toBeDefined()
})
it('should have valid image url', async () => {
const formData = new FormData()
const filePath = path.join(dirname, './image.svg')
const { file, handle } = await createStreamableFile(filePath)
formData.append('file', file)
const response = await restClient.POST(`/${mediaSlug}`, {
body: formData,
file,
})
const { doc } = await response.json()
await handle.close()
expect(response.status).toBe(201)
const expectedPath = path.join(dirname, './media')
expect(await fileExists(path.join(expectedPath, doc.filename))).toBe(true)
expect(doc.url).not.toContain('undefined')
})
it('creates images that do not require all sizes', async () => {
const formData = new FormData()
const filePath = path.join(dirname, './small.png')
const { file, handle } = await createStreamableFile(filePath)
formData.append('file', file)
const response = await restClient.POST(`/${mediaSlug}`, {
body: formData,
file,
})
const { doc } = await response.json()
await handle.close()
expect(response.status).toBe(201)
const expectedPath = path.join(dirname, './media')
// Check for files
expect(await fileExists(path.join(expectedPath, doc.filename))).toBe(true)
expect(await fileExists(path.join(expectedPath, 'small-640x480.png'))).toBe(false)
expect(await fileExists(path.join(expectedPath, doc.sizes.icon.filename))).toBe(true)
// Check api response
expect(doc.sizes.tablet.filename).toBeNull()
expect(doc.sizes.icon.filename).toBeDefined()
})
it('creates images from a different format', async () => {
const formData = new FormData()
const filePath = path.join(dirname, './image.jpg')
const { file, handle } = await createStreamableFile(filePath)
formData.append('file', file)
const response = await restClient.POST(`/${mediaSlug}`, {
body: formData,
file,
})
const { doc } = await response.json()
await handle.close()
expect(response.status).toBe(201)
const expectedPath = path.join(dirname, './media')
// Check for files
expect(await fileExists(path.join(expectedPath, doc.filename))).toBe(true)
expect(await fileExists(path.join(expectedPath, doc.sizes.tablet.filename))).toBe(true)
// Check api response
expect(doc.filename).toContain('.png')
expect(doc.mimeType).toEqual('image/png')
expect(doc.sizes.maintainedAspectRatio.filename).toContain('.png')
expect(doc.sizes.maintainedAspectRatio.mimeType).toContain('image/png')
expect(doc.sizes.differentFormatFromMainImage.filename).toContain('.jpg')
expect(doc.sizes.differentFormatFromMainImage.mimeType).toContain('image/jpeg')
})
it('creates media without storing a file', async () => {
const formData = new FormData()
const filePath = path.join(dirname, './unstored.png')
const { file, handle } = await createStreamableFile(filePath)
formData.append('file', file)
// unstored media
const response = await restClient.POST(`/${unstoredMediaSlug}`, {
body: formData,
file,
})
const { doc } = await response.json()
await handle.close()
expect(response.status).toBe(201)
// Check for files
expect(await fileExists(path.join(dirname, './media', doc.filename))).toBe(false)
// Check api response
expect(doc.filename).toBeDefined()
})
})
describe('update', () => {
it('should replace image and delete old files - by ID', async () => {
const filePath = path.resolve(dirname, './image.png')
const file = await getFileByPath(filePath)
file.name = 'renamed.png'
const mediaDoc = (await payload.create({
collection: mediaSlug,
data: {},
file,
})) as unknown as Media
const formData = new FormData()
const filePath2 = path.resolve(dirname, './small.png')
const { file: file2, handle } = await createStreamableFile(filePath2)
formData.append('file', file2)
const response = await restClient.PATCH(`/${mediaSlug}/${mediaDoc.id}`, {
body: formData,
file: file2,
})
await handle.close()
expect(response.status).toBe(200)
const expectedPath = path.join(dirname, './media')
// Check that previously existing files were removed
expect(await fileExists(path.join(expectedPath, mediaDoc.filename))).toBe(false)
expect(await fileExists(path.join(expectedPath, mediaDoc.sizes.icon.filename))).toBe(false)
})
it('should replace image and delete old files - where query', async () => {
const filePath = path.resolve(dirname, './image.png')
const file = await getFileByPath(filePath)
file.name = 'renamed.png'
const mediaDoc = (await payload.create({
collection: mediaSlug,
data: {},
file,
})) as unknown as Media
const formData = new FormData()
const filePath2 = path.resolve(dirname, './small.png')
const { file: file2, handle } = await createStreamableFile(filePath2)
formData.append('file', file2)
const response = await restClient.PATCH(`/${mediaSlug}`, {
body: formData,
file: file2,
query: {
where: {
id: {
equals: mediaDoc.id,
},
},
},
})
await handle.close()
expect(response.status).toBe(200)
const expectedPath = path.join(dirname, './media')
// Check that previously existing files were removed
expect(await fileExists(path.join(expectedPath, mediaDoc.filename))).toBe(false)
expect(await fileExists(path.join(expectedPath, mediaDoc.sizes.icon.filename))).toBe(false)
})
})
describe('delete', () => {
it('should remove related files when deleting by ID', async () => {
const formData = new FormData()
const filePath = path.join(dirname, './image.png')
const { file, handle } = await createStreamableFile(filePath)
formData.append('file', file)
const { doc } = await restClient
.POST(`/${mediaSlug}`, {
body: formData,
file,
})
.then((res) => res.json())
await handle.close()
const response2 = await restClient.DELETE(`/${mediaSlug}/${doc.id}`)
expect(response2.status).toBe(200)
expect(await fileExists(path.join(dirname, doc.filename))).toBe(false)
})
it('should remove all related files when deleting with where query', async () => {
const formData = new FormData()
const filePath = path.join(dirname, './image.png')
const { file, handle } = await createStreamableFile(filePath)
formData.append('file', file)
const { doc } = await restClient
.POST(`/${mediaSlug}`, {
body: formData,
file,
})
.then((res) => res.json())
await handle.close()
const { errors } = await restClient
.DELETE(`/${mediaSlug}`, {
query: {
where: {
id: {
equals: doc.id,
},
},
},
})
.then((res) => res.json())
expect(errors).toHaveLength(0)
expect(await fileExists(path.join(dirname, doc.filename))).toBe(false)
})
})
})
describe('Local API', () => {
describe('update', () => {
it('should remove existing media on re-upload - by ID', async () => {
// Create temp file
const filePath = path.resolve(dirname, './temp.png')
const file = await getFileByPath(filePath)
file.name = 'temp.png'
const mediaDoc = (await payload.create({
collection: mediaSlug,
data: {},
file,
})) as unknown as Media
const expectedPath = path.join(dirname, './media')
// Check that the temp file was created
expect(await fileExists(path.join(expectedPath, mediaDoc.filename))).toBe(true)
// Replace the temp file with a new one
const newFilePath = path.resolve(dirname, './temp-renamed.png')
const newFile = await getFileByPath(newFilePath)
newFile.name = 'temp-renamed.png'
const updatedMediaDoc = (await payload.update({
collection: mediaSlug,
id: mediaDoc.id,
file: newFile,
data: {},
})) as unknown as Media
// Check that the replacement file was created and the old one was removed
expect(await fileExists(path.join(expectedPath, updatedMediaDoc.filename))).toBe(true)
expect(await fileExists(path.join(expectedPath, mediaDoc.filename))).toBe(false)
})
it('should remove existing media on re-upload - where query', async () => {
// Create temp file
const filePath = path.resolve(dirname, './temp.png')
const file = await getFileByPath(filePath)
file.name = 'temp.png'
const mediaDoc = (await payload.create({
collection: mediaSlug,
data: {},
file,
})) as unknown as Media
const expectedPath = path.join(dirname, './media')
// Check that the temp file was created
expect(await fileExists(path.join(expectedPath, mediaDoc.filename))).toBe(true)
// Replace the temp file with a new one
const newFilePath = path.resolve(dirname, './temp-renamed.png')
const newFile = await getFileByPath(newFilePath)
newFile.name = 'temp-renamed-second.png'
const updatedMediaDoc = (await payload.update({
collection: mediaSlug,
where: {
id: { equals: mediaDoc.id },
},
file: newFile,
data: {},
})) as unknown as { docs: Media[] }
// Check that the replacement file was created and the old one was removed
expect(updatedMediaDoc.docs[0].filename).toEqual(newFile.name)
expect(await fileExists(path.join(expectedPath, updatedMediaDoc.docs[0].filename))).toBe(
true,
)
expect(await fileExists(path.join(expectedPath, mediaDoc.filename))).toBe(false)
})
it('should remove sizes that do not pertain to the new image - by ID', async () => {
const filePath = path.resolve(dirname, './image.png')
const file = await getFileByPath(filePath)
const small = await getFileByPath(path.resolve(dirname, './small.png'))
const { id } = await payload.create({
collection: mediaSlug,
data: {},
file,
})
const doc = (await payload.update({
collection: mediaSlug,
id,
data: {},
file: small,
})) as unknown as Media
expect(doc.sizes.icon).toBeDefined()
expect(doc.sizes.tablet.width).toBeNull()
})
it('should remove sizes that do not pertain to the new image - where query', async () => {
const filePath = path.resolve(dirname, './image.png')
const file = await getFileByPath(filePath)
const small = await getFileByPath(path.resolve(dirname, './small.png'))
const { id } = await payload.create({
collection: mediaSlug,
data: {},
file,
})
const doc = (await payload.update({
collection: mediaSlug,
where: {
id: { equals: id },
},
data: {},
file: small,
})) as unknown as { docs: Media[] }
expect(doc.docs[0].sizes.icon).toBeDefined()
expect(doc.docs[0].sizes.tablet.width).toBeNull()
})
it('should allow removing file from upload relationship field - by ID', async () => {
const filePath = path.resolve(dirname, './image.png')
const file = await getFileByPath(filePath)
file.name = 'renamed.png'
const { id } = await payload.create({
collection: mediaSlug,
data: {},
file,
})
const related = await payload.create({
collection: relationSlug,
data: {
image: id,
},
})
const doc = await payload.update({
collection: relationSlug,
id: related.id,
data: {
image: null,
},
})
expect(doc.image).toBeFalsy()
})
it('should allow update removing a relationship - where query', async () => {
const filePath = path.resolve(dirname, './image.png')
const file = await getFileByPath(filePath)
file.name = 'renamed.png'
const { id } = await payload.create({
collection: mediaSlug,
data: {},
file,
})
const related = await payload.create({
collection: relationSlug,
data: {
image: id,
},
})
const doc = await payload.update({
collection: relationSlug,
where: {
id: { equals: related.id },
},
data: {
image: null,
},
})
expect(doc.docs[0].image).toBeFalsy()
})
})
})
describe('focal point', () => {
let file
beforeAll(async () => {
// Create image
const filePath = path.resolve(dirname, './image.png')
file = await getFileByPath(filePath)
file.name = 'focal.png'
})
it('should be able to set focal point through local API', async () => {
const doc = await payload.create({
collection: focalOnlySlug,
data: {
focalX: 5,
focalY: 5,
},
file,
})
expect(doc.focalX).toEqual(5)
expect(doc.focalY).toEqual(5)
const updatedFocal = await payload.update({
collection: focalOnlySlug,
id: doc.id,
data: {
focalX: 10,
focalY: 10,
},
})
expect(updatedFocal.focalX).toEqual(10)
expect(updatedFocal.focalY).toEqual(10)
const updateWithoutFocal = await payload.update({
collection: focalOnlySlug,
id: doc.id,
data: {},
})
// Expect focal point to be the same
expect(updateWithoutFocal.focalX).toEqual(10)
expect(updateWithoutFocal.focalY).toEqual(10)
})
it('should default focal point to 50, 50', async () => {
const doc = await payload.create({
collection: focalOnlySlug,
data: {
// No focal point
},
file,
})
expect(doc.focalX).toEqual(50)
expect(doc.focalY).toEqual(50)
const updateWithoutFocal = await payload.update({
collection: focalOnlySlug,
id: doc.id,
data: {},
})
expect(updateWithoutFocal.focalX).toEqual(50)
expect(updateWithoutFocal.focalY).toEqual(50)
})
it('should set focal point even if no sizes defined', async () => {
const doc = await payload.create({
collection: focalNoSizesSlug, // config without sizes
data: {
// No focal point
},
file,
})
expect(doc.focalX).toEqual(50)
expect(doc.focalY).toEqual(50)
})
})
describe('Image Manipulation', () => {
it('should enlarge images if resize options `withoutEnlargement` is set to false', async () => {
const small = await getFileByPath(path.resolve(dirname, './small.png'))
const result = await payload.create({
collection: enlargeSlug,
data: {},
file: small,
})
expect(result).toBeTruthy()
const { sizes } = result as unknown as Enlarge
const expectedPath = path.join(dirname, './media/enlarge')
// Check for files
expect(await fileExists(path.join(expectedPath, small.name))).toBe(true)
expect(await fileExists(path.join(expectedPath, sizes.resizedLarger.filename))).toBe(true)
expect(await fileExists(path.join(expectedPath, sizes.resizedSmaller.filename))).toBe(true)
expect(await fileExists(path.join(expectedPath, sizes.accidentalSameSize.filename))).toBe(
true,
)
expect(await fileExists(path.join(expectedPath, sizes.sameSizeWithNewFormat.filename))).toBe(
true,
)
// Check api response
expect(sizes.sameSizeWithNewFormat.mimeType).toBe('image/jpeg')
expect(sizes.sameSizeWithNewFormat.filename).toBe('small-320x80.jpg')
expect(sizes.resizedLarger.mimeType).toBe('image/png')
expect(sizes.resizedLarger.filename).toBe('small-640x480.png')
expect(sizes.resizedSmaller.mimeType).toBe('image/png')
expect(sizes.resizedSmaller.filename).toBe('small-180x50.png')
expect(sizes.accidentalSameSize.mimeType).toBe('image/png')
expect(sizes.accidentalSameSize.filename).toBe('small-320x80.png')
await payload.delete({
collection: enlargeSlug,
id: result.id,
})
})
// This test makes sure that the image resizing is not prevented if only one dimension is larger (due to payload preventing enlargement by default)
it('should resize images if one desired dimension is smaller and the other is larger', async () => {
const small = await getFileByPath(path.resolve(dirname, './small.png'))
const result = (await payload.create({
collection: enlargeSlug,
data: {},
file: small,
})) as unknown as Enlarge
expect(result).toBeTruthy()
const { sizes } = result
const expectedPath = path.join(dirname, './media/enlarge')
// Check for files
expect(await fileExists(path.join(expectedPath, sizes.widthLowerHeightLarger.filename))).toBe(
true,
)
// Check api response
expect(sizes.widthLowerHeightLarger.mimeType).toBe('image/png')
expect(sizes.widthLowerHeightLarger.filename).toBe('small-300x300.png')
await payload.delete({
collection: enlargeSlug,
id: result.id,
})
})
it('should not reduce images if resize options `withoutReduction` is set to true', async () => {
const small = await getFileByPath(path.resolve(dirname, './small.png'))
const result = await payload.create({
collection: reduceSlug,
data: {},
file: small,
})
expect(result).toBeTruthy()
const { sizes } = result as unknown as Enlarge
const expectedPath = path.join(dirname, './media/reduce')
// Check for files
expect(await fileExists(path.join(expectedPath, small.name))).toBe(true)
expect(await fileExists(path.join(expectedPath, 'small-640x480.png'))).toBe(false)
expect(await fileExists(path.join(expectedPath, 'small-180x50.png'))).toBe(false)
expect(await fileExists(path.join(expectedPath, sizes.accidentalSameSize.filename))).toBe(
true,
)
expect(await fileExists(path.join(expectedPath, sizes.sameSizeWithNewFormat.filename))).toBe(
true,
)
// Check api response
expect(sizes.sameSizeWithNewFormat.mimeType).toBe('image/jpeg')
expect(sizes.sameSizeWithNewFormat.filename).toBe('small-320x80.jpg')
expect(sizes.resizedLarger.mimeType).toBeNull()
expect(sizes.resizedLarger.filename).toBeNull()
expect(sizes.accidentalSameSize.mimeType).toBe('image/png')
expect(sizes.resizedSmaller.filename).toBe('small-320x80.png')
expect(sizes.accidentalSameSize.mimeType).toBe('image/png')
expect(sizes.accidentalSameSize.filename).toBe('small-320x80.png')
})
})
describe('Required Files', () => {
it('should allow file to be optional if filesRequiredOnCreate is false', async () => {
const successfulCreate = await payload.create({
collection: 'optional-file',
data: {},
})
expect(successfulCreate.id).toBeDefined()
})
it('should throw an error if no file and filesRequiredOnCreate is true', async () => {
await expect(async () =>
payload.create({
collection: 'required-file',
data: {},
}),
).rejects.toThrow(
expect.objectContaining({
name: 'MissingFile',
message: 'No files were uploaded.',
}),
)
})
it('should throw an error if no file and filesRequiredOnCreate is not defined', async () => {
await expect(async () =>
payload.create({
collection: mediaSlug,
data: {},
}),
).rejects.toThrow(
expect.objectContaining({
name: 'MissingFile',
message: 'No files were uploaded.',
}),
)
})
})
})
async function fileExists(fileName: string): Promise<boolean> {
try {
await stat(fileName)
return true
} catch (err) {
return false
}
}