Skip to content

Commit dd52aa9

Browse files
committed
added tests for running theme commands
1 parent 5188073 commit dd52aa9

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import ThemeCommand from './theme-command.js'
2+
import {describe, vi, expect, test, beforeEach} from 'vitest'
3+
import {Config, Flags} from '@oclif/core'
4+
import {AdminSession, ensureAuthenticatedThemes} from '@shopify/cli-kit/node/session'
5+
import {loadEnvironment} from '@shopify/cli-kit/node/environments'
6+
import {renderConcurrent} from '@shopify/cli-kit/node/ui'
7+
import {Writable} from 'stream'
8+
9+
vi.mock('@shopify/cli-kit/node/session')
10+
vi.mock('@shopify/cli-kit/node/environments')
11+
vi.mock('@shopify/cli-kit/node/ui')
12+
13+
const CommandConfig = new Config({root: __dirname})
14+
15+
class TestThemeCommand extends ThemeCommand {
16+
static flags = {
17+
environment: Flags.string({
18+
multiple: true,
19+
default: [],
20+
env: 'SHOPIFY_FLAG_ENVIRONMENT',
21+
}),
22+
store: Flags.string({
23+
env: 'SHOPIFY_FLAG_STORE',
24+
}),
25+
password: Flags.string({
26+
env: 'SHOPIFY_FLAG_PASSWORD',
27+
}),
28+
}
29+
30+
static multiEnvironmentsFlags = ['store']
31+
32+
commandCalls: {flags: any; session: AdminSession; context?: any}[] = []
33+
34+
async command(flags: any, session: AdminSession, context?: {stdout?: Writable; stderr?: Writable}): Promise<void> {
35+
this.commandCalls.push({flags, session, context})
36+
}
37+
}
38+
39+
describe('ThemeCommand', () => {
40+
let mockSession: AdminSession
41+
42+
beforeEach(() => {
43+
mockSession = {
44+
token: 'test-token',
45+
storeFqdn: 'test-store.myshopify.com',
46+
}
47+
vi.mocked(ensureAuthenticatedThemes).mockResolvedValue(mockSession)
48+
})
49+
50+
describe('run', () => {
51+
test('no environment provided', async () => {
52+
// Given
53+
await CommandConfig.load()
54+
const command = new TestThemeCommand([], CommandConfig)
55+
56+
// When
57+
await command.run()
58+
59+
// Then
60+
expect(ensureAuthenticatedThemes).toHaveBeenCalledOnce()
61+
expect(loadEnvironment).not.toHaveBeenCalled()
62+
expect(renderConcurrent).not.toHaveBeenCalled()
63+
expect(command.commandCalls).toHaveLength(1)
64+
expect(command.commandCalls[0]).toMatchObject({
65+
flags: {environment: []},
66+
session: mockSession,
67+
context: undefined,
68+
})
69+
})
70+
71+
test('single environment provided', async () => {
72+
// Given
73+
const environmentConfig = {store: 'env-store.myshopify.com', theme: '123'}
74+
vi.mocked(loadEnvironment).mockResolvedValue(environmentConfig)
75+
76+
await CommandConfig.load()
77+
const command = new TestThemeCommand(['--environment', 'development'], CommandConfig)
78+
79+
// When
80+
await command.run()
81+
82+
// Then
83+
expect(loadEnvironment).toHaveBeenCalledWith('development', 'shopify.theme.toml')
84+
expect(ensureAuthenticatedThemes).toHaveBeenCalledTimes(1)
85+
expect(renderConcurrent).not.toHaveBeenCalled()
86+
expect(command.commandCalls).toHaveLength(1)
87+
expect(command.commandCalls[0]).toMatchObject({
88+
flags: {
89+
environment: ['development'],
90+
store: 'env-store.myshopify.com',
91+
theme: '123',
92+
},
93+
session: mockSession,
94+
context: undefined,
95+
})
96+
})
97+
98+
test('multiple environments provided - uses renderConcurrent for parallel execution', async () => {
99+
// Given
100+
const environmentConfig = {store: 'store.myshopify.com', theme: '123'}
101+
vi.mocked(loadEnvironment).mockResolvedValue(environmentConfig)
102+
vi.mocked(ensureAuthenticatedThemes).mockResolvedValue(mockSession)
103+
104+
vi.mocked(renderConcurrent).mockResolvedValue(undefined)
105+
106+
await CommandConfig.load()
107+
const command = new TestThemeCommand(['--environment', 'development', '--environment', 'staging'], CommandConfig)
108+
109+
// When
110+
await command.run()
111+
112+
// Then
113+
expect(loadEnvironment).toHaveBeenCalledWith('development', 'shopify.theme.toml')
114+
expect(loadEnvironment).toHaveBeenCalledWith('staging', 'shopify.theme.toml')
115+
expect(ensureAuthenticatedThemes).toHaveBeenCalledTimes(2)
116+
117+
expect(renderConcurrent).toHaveBeenCalledOnce()
118+
expect(renderConcurrent).toHaveBeenCalledWith(
119+
expect.objectContaining({
120+
processes: expect.arrayContaining([
121+
expect.objectContaining({prefix: 'development'}),
122+
expect.objectContaining({prefix: 'staging'}),
123+
]),
124+
showTimestamps: true,
125+
}),
126+
)
127+
})
128+
})
129+
})

0 commit comments

Comments
 (0)