Skip to content

Commit

Permalink
feat(customMiddleware): add test for custom middleware (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
lojzatran authored Jun 11, 2024
1 parent 55ba85d commit 101a31e
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import {
ClientBuilder as ClientBuilderV3,
MiddlewareRequest,
Next,
BeforeExecutionMiddlewareOptions,
AfterExecutionMiddlewareOptions,
ClientBuilder,
} from '@commercetools/ts-client/src'
import { createApiBuilderFromCtpClient } from '../../../src'
Expand Down Expand Up @@ -268,3 +266,31 @@ describe('Telemetry Middleware', () => {
expect(telemetryMiddlewareCalled).toHaveBeenCalled()
})
})

describe('Custom middleware', () => {
test('Should call custom middleware', async () => {
const customMiddlewareCalled = jest.fn()

const createMiddleware = () => {
return (next: Next) => {
return (req: MiddlewareRequest) => {
customMiddlewareCalled()
return next(req)
}
}
}
const client = new ClientBuilder()
.withClientCredentialsFlow(authMiddlewareOptions)
.withHttpMiddleware(httpMiddlewareOptionsV3)
.withMiddleware(createMiddleware())
.build()

const apiRootV3 = createApiBuilderFromCtpClient(client).withProjectKey({
projectKey,
})

await apiRootV3.get().execute()

expect(customMiddlewareCalled).toHaveBeenCalled()
})
})

0 comments on commit 101a31e

Please sign in to comment.