Skip to content

Commit

Permalink
test: unit test for wrapWithMutableAccessCheck and phase checks
Browse files Browse the repository at this point in the history
  • Loading branch information
lubieowoce committed Oct 11, 2024
1 parent 9766484 commit 04aa07d
Showing 1 changed file with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { RequestCookies } from '../cookies'
import {
workUnitAsyncStorage,
type RequestStore,
} from '../../../app-render/work-unit-async-storage.external'
import { RequestCookies, ResponseCookies } from '../cookies'
import {
ReadonlyRequestCookiesError,
RequestCookiesAdapter,
MutableRequestCookiesAdapter,
wrapWithMutableAccessCheck,
} from './request-cookies'

describe('RequestCookiesAdapter', () => {
Expand Down Expand Up @@ -100,3 +105,50 @@ describe('MutableRequestCookiesAdapter', () => {
])
})
})

describe('wrapWithMutableAccessCheck', () => {
const createMockRequestStore = (phase: RequestStore['phase']) =>
({ type: 'request', phase }) as RequestStore

it('prevents setting cookies in the render phase', () => {
const requestStore = createMockRequestStore('action')
workUnitAsyncStorage.run(requestStore, () => {
const headers = new Headers({})
const underlyingCookies = new ResponseCookies(headers)
const wrappedCookies = wrapWithMutableAccessCheck(underlyingCookies)

// simulate changing phases
requestStore.phase = 'render'

const EXPECTED_ERROR =
/Cookies can only be modified in a Server Action or Route Handler\./

expect(() => {
wrappedCookies.set('foo', '1')
}).toThrow(EXPECTED_ERROR)

expect(wrappedCookies.get('foo')).toBe(undefined)
})
})

it('prevents deleting cookies in the render phase', () => {
const requestStore = createMockRequestStore('action')
workUnitAsyncStorage.run(requestStore, () => {
const headers = new Headers({})
const underlyingCookies = new ResponseCookies(headers)
const wrappedCookies = wrapWithMutableAccessCheck(underlyingCookies)
wrappedCookies.set('foo', '1')

// simulate changing phases
requestStore.phase = 'render'

const EXPECTED_ERROR =
/Cookies can only be modified in a Server Action or Route Handler\./

expect(() => {
wrappedCookies.delete('foo')
}).toThrow(EXPECTED_ERROR)
expect(wrappedCookies.get('foo')?.value).toEqual('1')
})
})
})

0 comments on commit 04aa07d

Please sign in to comment.