|
| 1 | +import { fetchProduct } from './product' |
| 2 | +import { createContext } from '../mocks/contextFactory' |
| 3 | + |
| 4 | +// Mock the compareApiResults function |
| 5 | +jest.mock('../utils/compareResults', () => ({ |
| 6 | + compareApiResults: jest.fn().mockImplementation((func1) => func1()) |
| 7 | +})) |
| 8 | + |
| 9 | +describe('fetchProduct service', () => { |
| 10 | + const mockProduct = { |
| 11 | + productId: 'test-product', |
| 12 | + productName: 'Test Product', |
| 13 | + brand: 'Test Brand', |
| 14 | + } |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + jest.clearAllMocks() |
| 18 | + }) |
| 19 | + |
| 20 | + it('should use intsch directly for b2bstoreqa account', async () => { |
| 21 | + const ctx = createContext({ |
| 22 | + accountName: 'b2bstoreqa', |
| 23 | + }) |
| 24 | + |
| 25 | + // Mock the intsch client to return a product |
| 26 | + ctx.clients.intsch.fetchProduct = jest.fn().mockResolvedValue(mockProduct) |
| 27 | + |
| 28 | + const args = { |
| 29 | + identifier: { field: 'id' as const, value: 'test-id' }, |
| 30 | + salesChannel: 1, |
| 31 | + } |
| 32 | + |
| 33 | + const result = await fetchProduct(ctx as any, args) |
| 34 | + |
| 35 | + expect(ctx.clients.intsch.fetchProduct).toHaveBeenCalled() |
| 36 | + expect(result).toEqual([mockProduct]) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should use intsch directly for biggy account', async () => { |
| 40 | + const ctx = createContext({ |
| 41 | + accountName: 'biggy', |
| 42 | + }) |
| 43 | + |
| 44 | + // Mock the intsch client to return a product |
| 45 | + ctx.clients.intsch.fetchProduct = jest.fn().mockResolvedValue(mockProduct) |
| 46 | + |
| 47 | + const args = { |
| 48 | + identifier: { field: 'id' as const, value: 'test-id' }, |
| 49 | + salesChannel: 1, |
| 50 | + } |
| 51 | + |
| 52 | + const result = await fetchProduct(ctx as any, args) |
| 53 | + |
| 54 | + expect(ctx.clients.intsch.fetchProduct).toHaveBeenCalled() |
| 55 | + expect(result).toEqual([mockProduct]) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should use compareApiResults for other accounts', async () => { |
| 59 | + const { compareApiResults } = require('../utils/compareResults') |
| 60 | + const ctx = createContext({ |
| 61 | + accountName: 'regularaccount', |
| 62 | + }) |
| 63 | + |
| 64 | + // Mock the search client by casting to any |
| 65 | + ;(ctx.clients as any).search = { |
| 66 | + productById: jest.fn().mockResolvedValue([mockProduct]) |
| 67 | + } |
| 68 | + |
| 69 | + const args = { |
| 70 | + identifier: { field: 'id' as const, value: 'test-id' }, |
| 71 | + salesChannel: 1, |
| 72 | + } |
| 73 | + |
| 74 | + await fetchProduct(ctx as any, args) |
| 75 | + |
| 76 | + expect(compareApiResults).toHaveBeenCalled() |
| 77 | + }) |
| 78 | +}) |
0 commit comments