Skip to content
Open
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
85 changes: 85 additions & 0 deletions packages/app/src/cli/services/store-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,91 @@ describe('storeContext', () => {
})
})

test('does not update hidden config when cached URL differs only by format', async () => {
await inTemporaryDirectory(async (dir) => {
// The TOML has an un-normalized URL (with https:// prefix)
const appWithUnnormalizedUrl = testAppLinked({
configuration: {
client_id: 'client_id',
name: 'app-config-name',
build: {
dev_store_url: 'https://test-store.myshopify.com',
},
} as any,
hiddenConfig: {
dev_store_url: 'test-store.myshopify.com',
},
})
const updatedAppContextResult = {...appContextResult, app: appWithUnnormalizedUrl}
const storeMatchingNormalized = testOrganizationStore({
shopId: 'store1',
shopDomain: 'test-store.myshopify.com',
})

await prepareAppFolder(appWithUnnormalizedUrl, dir)
vi.mocked(fetchStore).mockResolvedValue(storeMatchingNormalized)

await storeContext({appContextResult: updatedAppContextResult, forceReselectStore: false})

// The hidden config should NOT be updated since the normalized URLs match
// and the hidden config already has a value
const hiddenConfig = await readFile(appHiddenConfigPath(dir))
// The file was initialized empty, so if updateHiddenConfig was NOT called,
// it should still be empty
expect(hiddenConfig).toEqual('')
})
Comment on lines +234 to +238
})

test('does not update hidden config when cached URL is a short store name', async () => {
await inTemporaryDirectory(async (dir) => {
// The TOML has just the store name without .myshopify.com
const appWithShortUrl = testAppLinked({
configuration: {
client_id: 'client_id',
name: 'app-config-name',
build: {
dev_store_url: 'test-store',
},
} as any,
hiddenConfig: {
dev_store_url: 'test-store.myshopify.com',
},
})
const updatedAppContextResult = {...appContextResult, app: appWithShortUrl}
const storeMatchingNormalized = testOrganizationStore({
shopId: 'store1',
shopDomain: 'test-store.myshopify.com',
})

await prepareAppFolder(appWithShortUrl, dir)
vi.mocked(fetchStore).mockResolvedValue(storeMatchingNormalized)

await storeContext({appContextResult: updatedAppContextResult, forceReselectStore: false})

// The hidden config should NOT be updated since normalized URLs match
const hiddenConfig = await readFile(appHiddenConfigPath(dir))
expect(hiddenConfig).toEqual('')
Comment on lines +263 to +269
})
})

test('updates hidden config when store actually changes', async () => {
await inTemporaryDirectory(async (dir) => {
await prepareAppFolder(mockApp, dir)
const differentStore = testOrganizationStore({
shopId: 'store2',
shopDomain: 'different-store.myshopify.com',
})
vi.mocked(fetchStore).mockResolvedValue(differentStore)

await storeContext({appContextResult, forceReselectStore: false})

const hiddenConfig = await readFile(appHiddenConfigPath(dir))
expect(hiddenConfig).toEqual(
'{\n "client_id": {\n "dev_store_url": "different-store.myshopify.com"\n }\n}',
)
})
})

test('ensures user access to store', async () => {
await inTemporaryDirectory(async (dir) => {
await prepareAppFolder(mockApp, dir)
Expand Down
8 changes: 6 additions & 2 deletions packages/app/src/cli/services/store-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ export async function storeContext({
await logMetadata(selectedStore, forceReselectStore)
selectedStore.shopDomain = normalizeStoreFqdn(selectedStore.shopDomain)

// Save the selected store in the hidden config file
if (selectedStore.shopDomain !== cachedStoreURL || !devStoreUrlFromHiddenConfig) {
// Save the selected store in the hidden config file.
// Normalize the cached URL before comparing so that format differences
// (e.g. "https://store.myshopify.com" vs "store.myshopify.com") don't
// cause unnecessary writes.
const normalizedCachedURL = cachedStoreURL ? normalizeStoreFqdn(cachedStoreURL) : undefined
if (selectedStore.shopDomain !== normalizedCachedURL || !devStoreUrlFromHiddenConfig) {
await app.updateHiddenConfig({dev_store_url: selectedStore.shopDomain})
}

Expand Down
Loading