Skip to content

Commit

Permalink
test(releases): adding test cases for archived and publish retention …
Browse files Browse the repository at this point in the history
…cards
  • Loading branch information
jordanl17 committed Feb 11, 2025
1 parent ad61a1b commit 4a8b38d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {type Mock, type Mocked} from 'vitest'

import {useProjectSubscriptions} from '../useProjectSubscriptions'

export const useProjectSubscriptionsMockReturn: Mocked<ReturnType<typeof useProjectSubscriptions>> =
{
error: null,
isLoading: false,
projectSubscriptions: {
id: 'sub_123',
projectId: 'proj_456',
productType: 'premium',
productId: 'prod_789',
customerId: 'cust_101',
planId: 'plan_202',
previousSubscriptionId: null,
status: 'active',
startedAt: '2024-02-01T00:00:00Z',
startedBy: 'user_303',
endedAt: null,
endedBy: null,
trialUntil: '2024-02-15T00:00:00Z',
plan: {
id: 'plan_202',
planTypeId: 'type_404',
variantId: null,
productType: 'premium',
variantOfPlanId: null,
name: 'Premium Plan',
variantName: null,
price: 49.99,
trialDays: 14,
createdAt: '2024-01-01T00:00:00Z',
supersededAt: null,
default: false,
public: true,
orderable: true,
isBasePlan: true,
pricingModel: 'flat-rate',
resources: {},
featureTypes: {},
},
resources: {},
featureTypes: {
retention: {
features: [
{
attributes: {
maxRetentionDays: 123,
},
id: '',
variantId: null,
name: '',
price: 0,
included: false,
custom: false,
startedAt: null,
startedBy: null,
endedAt: null,
endedBy: null,
},
],
id: '',
name: '',
singular: false,
},
},
},
}

export const mockUseProjectSubscriptions = useProjectSubscriptions as Mock<
typeof useProjectSubscriptions
>
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function ReleaseDashboardDetails({release}: {release: ReleaseDocument}) {
)}

{!isReleaseOpen && retentionDays && (
<Card padding={4} radius={4} tone="primary">
<Card padding={4} radius={4} tone="primary" data-testid="retention-policy-card">
<Flex gap={3}>
<Text size={1}>
<InfoOutlineIcon />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {act, fireEvent, render, screen, waitFor} from '@testing-library/react'
import {act, fireEvent, render, screen, waitFor, within} from '@testing-library/react'
import {route, RouterProvider} from 'sanity/router'
import {beforeEach, describe, expect, it, vi} from 'vitest'

import {mockUseRouterReturn} from '../../../../../../test/mocks/useRouter.mock'
import {createTestProvider} from '../../../../../../test/testUtils/TestProvider'
import {useProjectSubscriptionsMockReturn} from '../../../../hooks/__mocks__/useProjectSubscriptions.mock'
import {
activeASAPRelease,
activeUndecidedErrorRelease,
archivedScheduledRelease,
publishedASAPRelease,
} from '../../../__fixtures__/release.fixture'
import {releasesUsEnglishLocaleBundle} from '../../../i18n'
Expand Down Expand Up @@ -66,6 +68,10 @@ vi.mock('../documentTable/useReleaseHistory', () => ({
}),
}))

vi.mock('../../../../hooks/useProjectSubscriptions', () => ({
useProjectSubscriptions: vi.fn(() => useProjectSubscriptionsMockReturn),
}))

const mockRouterNavigate = vi.fn()

const renderTest = async () => {
Expand Down Expand Up @@ -273,6 +279,47 @@ describe('after releases have loaded', () => {
})
})

describe('with archived release', () => {
beforeEach(async () => {
mockUseActiveReleases.mockReset()

mockUseActiveReleases.mockReturnValue({
...useActiveReleasesMockReturn,
data: [archivedScheduledRelease],
})

mockUseRouterReturn.state = {
releaseId: getReleaseIdFromReleaseDocumentId(archivedScheduledRelease._id),
}

await renderTest()
})

publishAgnosticTests(archivedScheduledRelease.metadata.title)

it('allows for navigating back to archived overview', () => {
fireEvent.click(screen.getByTestId('back-to-releases-button'))

expect(mockUseRouterReturn.navigate).toHaveBeenCalledWith({
_searchParams: [['group', 'archived']],
})
})

it('should show archived retention card', () => {
screen.getByText('This release is archived')

within(screen.getByTestId('retention-policy-card')).getByText('123', {exact: false})
})

it('should not show a schedule date or release type', () => {
expect(screen.queryByTestId('release-type-label')).not.toBeInTheDocument()
})

it('should not show the pin release button', () => {
expect(screen.queryByText('Pin release')).not.toBeInTheDocument()
})
})

describe('with published release', () => {
beforeEach(async () => {
mockUseActiveReleases.mockReset()
Expand All @@ -291,6 +338,24 @@ describe('after releases have loaded', () => {

publishAgnosticTests(publishedASAPRelease.metadata.title)

it('allows for navigating back to archived overview', () => {
fireEvent.click(screen.getByTestId('back-to-releases-button'))

expect(mockUseRouterReturn.navigate).toHaveBeenCalledWith({
_searchParams: [['group', 'archived']],
})
})

it('should show published retention card', () => {
screen.getByText('This release is published')

within(screen.getByTestId('retention-policy-card')).getByText('123', {exact: false})
})

it('should not show the pin release button', () => {
expect(screen.queryByText('Pin release')).not.toBeInTheDocument()
})

it('should not show the publish button', () => {
expect(screen.queryByText('Publish all')).toBeNull()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,6 @@ describe('ReleaseTypePicker', () => {
})

describe('picker behavior based on release state', () => {
it('disables the picker for archived releases', async () => {
await renderComponent({...activeASAPRelease, state: 'archived'})

const pickerButton = screen.getByRole('button')
expect(pickerButton).toBeDisabled()
})

it('does not show button for picker when release is published state', async () => {
await renderComponent(publishedASAPRelease)

Expand Down

0 comments on commit 4a8b38d

Please sign in to comment.