dify/web/context/provider-context-mock.spec.tsx
Stephen Zhou eabdc5f0eb
Some checks are pending
autofix.ci / autofix (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Waiting to run
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Waiting to run
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Waiting to run
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Blocked by required conditions
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Blocked by required conditions
Main CI Pipeline / Check Changed Files (push) Waiting to run
Main CI Pipeline / API Tests (push) Blocked by required conditions
Main CI Pipeline / Web Tests (push) Blocked by required conditions
Main CI Pipeline / Style Check (push) Waiting to run
Main CI Pipeline / VDB Tests (push) Blocked by required conditions
Main CI Pipeline / DB Migration Test (push) Blocked by required conditions
refactor(web): migrate to Vitest and esm (#29974)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: yyh <yuanyouhuilyz@gmail.com>
2025-12-22 16:35:22 +08:00

82 lines
2.5 KiB
TypeScript

import { render } from '@testing-library/react'
import type { UsagePlanInfo } from '@/app/components/billing/type'
import { Plan } from '@/app/components/billing/type'
import ProviderContextMock from './provider-context-mock'
import { createMockPlan, createMockPlanReset, createMockPlanTotal, createMockPlanUsage } from '@/__mocks__/provider-context'
let mockPlan: Plan = Plan.sandbox
const usage: UsagePlanInfo = {
vectorSpace: 1,
buildApps: 10,
teamMembers: 1,
annotatedResponse: 1,
documentsUploadQuota: 0,
apiRateLimit: 0,
triggerEvents: 0,
}
const total: UsagePlanInfo = {
vectorSpace: 100,
buildApps: 100,
teamMembers: 10,
annotatedResponse: 100,
documentsUploadQuota: 0,
apiRateLimit: 0,
triggerEvents: 0,
}
const reset = {
apiRateLimit: 100,
triggerEvents: 100,
}
vi.mock('@/context/provider-context', () => ({
useProviderContext: () => {
const withPlan = createMockPlan(mockPlan)
const withUsage = createMockPlanUsage(usage, withPlan)
const withTotal = createMockPlanTotal(total, withUsage)
const withReset = createMockPlanReset(reset, withTotal)
return withReset
},
}))
const renderWithPlan = (plan: Plan) => {
mockPlan = plan
return render(<ProviderContextMock />)
}
describe('ProviderContextMock', () => {
beforeEach(() => {
mockPlan = Plan.sandbox
vi.clearAllMocks()
})
it('should display sandbox plan type when mocked with sandbox plan', async () => {
const { getByTestId } = renderWithPlan(Plan.sandbox)
expect(getByTestId('plan-type').textContent).toBe(Plan.sandbox)
})
it('should display team plan type when mocked with team plan', () => {
const { getByTestId } = renderWithPlan(Plan.team)
expect(getByTestId('plan-type').textContent).toBe(Plan.team)
})
it('should provide usage info from mocked plan', () => {
const { getByTestId } = renderWithPlan(Plan.team)
const buildApps = getByTestId('plan-usage-build-apps').textContent
expect(Number(buildApps as string)).toEqual(usage.buildApps)
})
it('should provide total info from mocked plan', () => {
const { getByTestId } = renderWithPlan(Plan.team)
const buildApps = getByTestId('plan-total-build-apps').textContent
expect(Number(buildApps as string)).toEqual(total.buildApps)
})
it('should provide reset info from mocked plan', () => {
const { getByTestId } = renderWithPlan(Plan.team)
const apiRateLimit = getByTestId('plan-reset-api-rate-limit').textContent
expect(Number(apiRateLimit as string)).toEqual(reset.apiRateLimit)
})
})