Skip to content

Commit

Permalink
Merge pull request #3821 from Shopify/jmeng/devcleanup_test
Browse files Browse the repository at this point in the history
[Themes] Improve clarity of tests for push command
  • Loading branch information
jamesmengo authored Apr 30, 2024
2 parents ab5ebec + 16401bc commit f99b167
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions packages/theme/src/cli/commands/theme/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {UnpublishedThemeManager} from '../../utilities/unpublished-theme-manager
import {ensureThemeStore} from '../../utilities/theme-store.js'
import {findOrSelectTheme} from '../../utilities/theme-selector.js'
import {push} from '../../services/push.js'
import {FilterProps} from '../../utilities/theme-selector/filter.js'
import {describe, vi, expect, test, beforeEach} from 'vitest'
import {describe, vi, expect, test} from 'vitest'
import {Config} from '@oclif/core'
import {execCLI2} from '@shopify/cli-kit/node/ruby'
import {AdminSession, ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session'
Expand Down Expand Up @@ -55,29 +54,13 @@ describe('Push', () => {
})

describe('createOrSelectTheme', async () => {
beforeEach(() => {
vi.mocked(DevelopmentThemeManager.prototype.findOrCreate).mockResolvedValue(
buildTheme({id: 1, name: 'Theme', role: DEVELOPMENT_THEME_ROLE})!,
)
test('creates unpublished theme when unpublished flag is provided', async () => {
// Given
vi.mocked(UnpublishedThemeManager.prototype.create).mockResolvedValue(
buildTheme({id: 2, name: 'Unpublished Theme', role: UNPUBLISHED_THEME_ROLE})!,
)
vi.mocked(findOrSelectTheme).mockImplementation(
async (_session: AdminSession, options: {header?: string; filter: FilterProps}) => {
if (options.filter.live) {
return buildTheme({id: 3, name: 'Live Theme', role: LIVE_THEME_ROLE})!
} else if (options.filter.theme) {
return buildTheme({id: 4, name: options.filter.theme, role: DEVELOPMENT_THEME_ROLE})!
} else {
return buildTheme({id: 5, name: 'Theme', role: DEVELOPMENT_THEME_ROLE})!
}
},
)
})

test('creates unpublished theme when unpublished flag is provided', async () => {
// Given
const flags: ThemeSelectionOptions = {unpublished: true}

// When
const theme = await createOrSelectTheme(adminSession, flags)

Expand All @@ -88,7 +71,11 @@ describe('Push', () => {

test('creates development theme when development flag is provided', async () => {
// Given
vi.mocked(DevelopmentThemeManager.prototype.findOrCreate).mockResolvedValue(
buildTheme({id: 1, name: 'Theme', role: DEVELOPMENT_THEME_ROLE})!,
)
const flags: ThemeSelectionOptions = {development: true}

// When
const theme = await createOrSelectTheme(adminSession, flags)

Expand All @@ -97,32 +84,37 @@ describe('Push', () => {
expect(DevelopmentThemeManager.prototype.findOrCreate).toHaveBeenCalled()
})

test('returns live theme when live flag is provided', async () => {
test('creates development theme when development and unpublished flags are provided', async () => {
// Given
const flags: ThemeSelectionOptions = {live: true, 'allow-live': true}
vi.mocked(DevelopmentThemeManager.prototype.findOrCreate).mockResolvedValue(
buildTheme({id: 1, name: 'Theme', role: DEVELOPMENT_THEME_ROLE})!,
)
const flags: ThemeSelectionOptions = {development: true, unpublished: true}

// When
const theme = await createOrSelectTheme(adminSession, flags)

// Then
expect(theme).toMatchObject({role: LIVE_THEME_ROLE})
expect(theme).toMatchObject({role: DEVELOPMENT_THEME_ROLE})
})

test('creates development theme when development and unpublished flags are provided', async () => {
test('returns live theme when live flag is provided', async () => {
// Given
const flags: ThemeSelectionOptions = {development: true, unpublished: true}
vi.mocked(findOrSelectTheme).mockResolvedValue(buildTheme({id: 3, name: 'Live Theme', role: LIVE_THEME_ROLE})!)
const flags: ThemeSelectionOptions = {live: true, 'allow-live': true}

// When
const theme = await createOrSelectTheme(adminSession, flags)

// Then
expect(theme).toMatchObject({role: DEVELOPMENT_THEME_ROLE})
expect(theme).toMatchObject({role: LIVE_THEME_ROLE})
})

test("renders confirmation prompt if 'allow-live' flag is not provided and selected theme role is live", async () => {
// Given
const flags: ThemeSelectionOptions = {live: true}
vi.mocked(findOrSelectTheme).mockResolvedValue(buildTheme({id: 3, name: 'Live Theme', role: LIVE_THEME_ROLE})!)
vi.mocked(renderConfirmationPrompt).mockResolvedValue(true)
const flags: ThemeSelectionOptions = {live: true}

// When
const theme = await createOrSelectTheme(adminSession, flags)
Expand All @@ -134,9 +126,9 @@ describe('Push', () => {

test("renders confirmation prompt if 'allow-live' flag is not provided and live theme is specified via theme flag", async () => {
// Given
const flags: ThemeSelectionOptions = {theme: '3'}
vi.mocked(findOrSelectTheme).mockResolvedValue(buildTheme({id: 3, name: 'Live Theme', role: LIVE_THEME_ROLE})!)
vi.mocked(renderConfirmationPrompt).mockResolvedValue(true)
const flags: ThemeSelectionOptions = {theme: '3'}

// When
const theme = await createOrSelectTheme(adminSession, flags)
Expand All @@ -148,8 +140,9 @@ describe('Push', () => {

test('returns undefined if live theme confirmation prompt is not confirmed', async () => {
// Given
const flags: ThemeSelectionOptions = {live: true}
vi.mocked(findOrSelectTheme).mockResolvedValue(buildTheme({id: 3, name: 'Live Theme', role: LIVE_THEME_ROLE})!)
vi.mocked(renderConfirmationPrompt).mockResolvedValue(false)
const flags: ThemeSelectionOptions = {live: true}

// When
const theme = await createOrSelectTheme(adminSession, flags)
Expand All @@ -158,28 +151,31 @@ describe('Push', () => {
expect(theme).toBeUndefined()
})

test('renders text prompt if unpublished flag is provided and theme flag is not provided', async () => {
test('returns undefined if confirmation prompt is rejected', async () => {
// Given
const flags = {unpublished: true}
vi.mocked(findOrSelectTheme).mockResolvedValue(buildTheme({id: 3, name: 'Live Theme', role: LIVE_THEME_ROLE})!)
vi.mocked(renderConfirmationPrompt).mockResolvedValue(false)
const flags = {live: true}

// When
await createOrSelectTheme(adminSession, flags)
const theme = await createOrSelectTheme(adminSession, flags)

// Then
expect(renderTextPrompt).toHaveBeenCalled()
expect(theme).toBeUndefined()
})

test('returns undefined if confirmation prompt is rejected', async () => {
test('renders text prompt if unpublished flag is provided and theme flag is not provided', async () => {
// Given
const flags = {live: true}

vi.mocked(renderConfirmationPrompt).mockResolvedValue(false)
const flags = {unpublished: true}

// When
const theme = await createOrSelectTheme(adminSession, flags)
await createOrSelectTheme(adminSession, flags)

// Then
expect(theme).toBeUndefined()
expect(renderTextPrompt).toHaveBeenCalledWith({
message: 'Name of the new theme',
defaultValue: expect.any(String),
})
})
})

Expand Down

0 comments on commit f99b167

Please sign in to comment.