Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/pages/project/instances/InstancesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { Tooltip } from '~/ui/lib/Tooltip'
import { setDiff } from '~/util/array'
import { toLocaleTimeString } from '~/util/date'
import { pb } from '~/util/path-builder'
import { pluralize } from '~/util/str'

import { useMakeInstanceActions } from './actions'
import { ResizeInstanceModal } from './instance/InstancePage'
Expand Down Expand Up @@ -99,7 +100,8 @@ export function Component() {
header: 'CPU',
cell: (info) => (
<>
{info.getValue()} <span className="ml-1 text-tertiary">vCPU</span>
{info.getValue()}{' '}
<span className="ml-1 text-tertiary">{pluralize('vCPU', info.getValue())}</span>
</>
),
}),
Expand Down
3 changes: 2 additions & 1 deletion app/pages/project/instances/instance/InstancePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { Spinner } from '~/ui/lib/Spinner'
import { Tooltip } from '~/ui/lib/Tooltip'
import { truncate } from '~/ui/lib/Truncate'
import { pb } from '~/util/path-builder'
import { pluralize } from '~/util/str'
import { GiB } from '~/util/units'

import { useMakeInstanceActions } from '../actions'
Expand Down Expand Up @@ -221,7 +222,7 @@ export function InstancePage() {
<PropertiesTable>
<PropertiesTable.Row label="cpu">
<span className="text-default">{instance.ncpus}</span>
<span className="ml-1 text-tertiary"> vCPUs</span>
<span className="ml-1 text-tertiary">{pluralize(' vCPU', instance.ncpus)}</span>
</PropertiesTable.Row>
<PropertiesTable.Row label="ram">
<span className="text-default">{memory.value}</span>
Expand Down
12 changes: 11 additions & 1 deletion app/util/str.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
extractText,
kebabCase,
normalizeName,
pluralize,
titleCase,
} from './str'

Expand All @@ -23,6 +24,14 @@ describe('capitalize', () => {
})
})

describe('pluralize', () => {
it('pluralizes correctly', () => {
expect(pluralize('item', 0)).toBe('items')
expect(pluralize('item', 1)).toBe('item')
expect(pluralize('item', 2)).toBe('items')
})
})

describe('camelCase', () => {
it('basic formats to camel case', () => {
expect(camelCase('camelCase')).toBe('camelCase')
Expand Down Expand Up @@ -51,8 +60,9 @@ it('commaSeries', () => {
expect(commaSeries([], 'or')).toBe('')
expect(commaSeries(['a'], 'or')).toBe('a')
expect(commaSeries(['a', 'b'], 'or')).toBe('a or b')
expect(commaSeries(['a', 'b'], 'or')).toBe('a or b')
expect(commaSeries(['a', 'b'], 'and')).toBe('a and b')
expect(commaSeries(['a', 'b', 'c'], 'or')).toBe('a, b, or c')
expect(commaSeries(['a', 'b', 'c'], 'and')).toBe('a, b, and c')
})

describe('titleCase', () => {
Expand Down
2 changes: 1 addition & 1 deletion app/util/str.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React, { type ReactElement, type ReactNode } from 'react'

export const capitalize = (s: string) => s && s.charAt(0).toUpperCase() + s.slice(1)

export const pluralize = (s: string, n: number) => `${n} ${s}${n === 1 ? '' : 's'}`
export const pluralize = (s: string, n: number) => `${s}${n === 1 ? '' : 's'}`

export const camelCase = (s: string) =>
s
Expand Down
14 changes: 7 additions & 7 deletions test/e2e/instance.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ test('can resize a failed or stopped instance', async ({ page }) => {
// resize 'you-fail', currently in a failed state
await expectRowVisible(table, {
name: 'you-fail',
CPU: '4 vCPU',
CPU: '4 vCPUs',
Memory: '6 GiB',
state: expect.stringMatching(/^failed\d+s$/),
})
Expand All @@ -173,15 +173,15 @@ test('can resize a failed or stopped instance', async ({ page }) => {
await resizeModal.getByRole('button', { name: 'Resize' }).click()
await expectRowVisible(table, {
name: 'you-fail',
CPU: '10 vCPU',
CPU: '10 vCPUs',
Memory: '20 GiB',
state: expect.stringMatching(/^failed\d+s$/),
})

// resize 'db1', which needs to be stopped first
await expectRowVisible(table, {
name: 'db1',
CPU: '2 vCPU',
CPU: '2 vCPUs',
Memory: '4 GiB',
state: expect.stringMatching(/^running\d+s$/),
})
Expand All @@ -200,7 +200,7 @@ test('can resize a failed or stopped instance', async ({ page }) => {
await resizeModal.getByRole('button', { name: 'Resize' }).click()
await expectRowVisible(table, {
name: 'db1',
CPU: '8 vCPU',
CPU: '8 vCPUs',
Memory: '16 GiB',
state: expect.stringMatching(/^stopped\d+s$/),
})
Expand All @@ -224,19 +224,19 @@ test('instance table', async ({ page }) => {
const table = page.getByRole('table')
await expectRowVisible(table, {
name: 'db1',
CPU: '2 vCPU',
CPU: '2 vCPUs',
Memory: '4 GiB',
state: expect.stringMatching(/^running\d+s$/),
})
await expectRowVisible(table, {
name: 'you-fail',
CPU: '4 vCPU',
CPU: '4 vCPUs',
Memory: '6 GiB',
state: expect.stringMatching(/^failed\d+s$/),
})
await expectRowVisible(table, {
name: 'not-there-yet',
CPU: '2 vCPU',
CPU: '2 vCPUs',
Memory: '8 GiB',
state: expect.stringMatching(/^starting\d+s$/),
})
Expand Down
Loading