/* eslint-env jest */ import { approvedVariantCount, approvedVariantKey, brandMode, canSaveDraft, changedRuleCount, filterGroups, filterValue, groupOptions, missingSettings, pageCount, productMode, rulesKey, selectionRules, standingText, withBrandMode, withListValue, withProductMode, withRuleValue, } from '../rules'; import { ANY_FILTER_VALUE, EMPTY_RULES, } from '../../../service/ai-shopping-feeds/ai-shopping-feeds.constants'; import type { IFeedGroup, IFeedProductRow, } from '../../../service/ai-shopping-feeds/ai-shopping-feeds.interface'; const GROUPS: IFeedGroup[] = [ { id: 'Acme', name: 'Acme', product_count: 3, listed_count: 3 }, { id: 'Ergo', name: 'Ergo', product_count: 2, listed_count: 0 }, ]; const VARIANT = { id: '11', label: 'Blue', sku: 'A-BL', image_url: '', price: '79.99 USD', }; /** * A product row as the search answers it. * * @param {Partial} fields - What differs from a product listed with every variation by default. * @return {IFeedProductRow} The row. */ function productRow(fields: Partial): IFeedProductRow { return { id: '785', name: 'Chair', brand: 'Acme', url: '', image_url: '', categories: [], is_new: false, has_own_rule: false, listing: 'every', reason: 'default', reason_name: '', variants: [VARIANT, { ...VARIANT, id: '12', label: 'Red' }], ...fields, }; } describe('ai shopping feed rules', () => { it('sets and clears a list value once, sorted', () => { expect(withListValue(['b'], 'a', true)).toEqual(['a', 'b']); expect(withListValue(['a', 'b'], 'a', false)).toEqual(['b']); expect(withListValue(['a'], 'a', true)).toEqual(['a']); }); it('moves a brand between leaving out and one listing, without case', () => { const excluded = withBrandMode(EMPTY_RULES, 'Alera', 'excluded'); expect(brandMode(excluded, 'alera')).toBe('excluded'); const parent = withBrandMode( { ...excluded, excluded_brands: ['alera'] }, 'Alera', 'parent' ); expect(parent.excluded_brands).toEqual([]); expect(brandMode(parent, 'Alera')).toBe('parent'); expect(brandMode(withBrandMode(parent, 'Alera', 'default'), 'Alera')).toBe( 'default' ); }); it('gives a product one rule and drops its approved variations when it is no longer listed once', () => { const parent = withProductMode(EMPTY_RULES, '785', 'parent'); const approved = withRuleValue( parent, 'approved_variants', approvedVariantKey('785', '12'), true ); expect(productMode(approved, '785')).toBe('parent'); expect(approved.approved_variants).toEqual(['785:12']); const full = withProductMode(approved, '785', 'full'); expect(productMode(full, '785')).toBe('full'); expect(full.parent_products).toEqual([]); expect(full.approved_variants).toEqual([]); expect(productMode(withProductMode(full, '785', 'excluded'), '785')).toBe( 'excluded' ); }); it('saves only the rules previewed, with a seller', () => { const draft = { ...EMPTY_RULES, seller_name: 'Acme', excluded_brands: ['b', 'a'], }; const reordered = { ...draft, excluded_brands: ['a', 'b'] }; expect(rulesKey(draft)).toBe(rulesKey(reordered)); expect(canSaveDraft(reordered, rulesKey(draft))).toBe(true); expect(canSaveDraft({ ...draft, country: 'GB' }, rulesKey(draft))).toBe( false ); expect( canSaveDraft( { ...draft, seller_name: ' ' }, rulesKey({ ...draft, seller_name: ' ' }) ) ).toBe(false); expect(canSaveDraft(draft, undefined)).toBe(false); }); it('finds brands or categories by name and lists them as filter choices after every one', () => { expect(filterGroups(GROUPS, ' erg').map(group => group.id)).toEqual([ 'Ergo', ]); expect(filterGroups(GROUPS, ' ')).toHaveLength(2); expect(groupOptions(GROUPS, 'Every brand')).toEqual([ { value: ANY_FILTER_VALUE, label: 'Every brand' }, { value: 'Acme', label: 'Acme' }, { value: 'Ergo', label: 'Ergo' }, ]); expect(filterValue(ANY_FILTER_VALUE)).toBe(''); expect(filterValue(null)).toBe(''); expect(filterValue('Ergo')).toBe('Ergo'); }); it('says how a product stands and what decided it', () => { expect(standingText(productRow({}), 0)).toBe( 'Every variation (2) · the default' ); expect( standingText( productRow({ listing: 'excluded', reason: 'brand', reason_name: 'Alera', }), 0 ) ).toBe('Left out · brand Alera'); expect( standingText(productRow({ listing: 'one', reason: 'product' }), 1) ).toBe('Listed once + 1 on their own · its own rule'); expect( standingText(productRow({ listing: 'held', reason: 'new' }), 0) ).toBe('Waiting for your approval · new since your last save'); const approved = withRuleValue( EMPTY_RULES, 'approved_variants', approvedVariantKey('785', '12'), true ); expect(approvedVariantCount(approved, '785')).toBe(1); expect(approvedVariantCount(approved, '78')).toBe(0); }); it('keeps only the rules that decide the listing, so the name and country do not search again', () => { const rules = { ...EMPTY_RULES, seller_name: 'Acme', country: 'GB', excluded_brands: ['Alera'], }; const selection = selectionRules(rules); expect(selection).not.toHaveProperty('seller_name'); expect(selection).not.toHaveProperty('country'); expect(selection.excluded_brands).toEqual(['Alera']); expect( JSON.stringify(selectionRules({ ...rules, seller_name: 'Acme Ltd' })) ).toBe(JSON.stringify(selection)); }); it('asks for the business name and a two-letter country before a preview', () => { expect(missingSettings(EMPTY_RULES)).toBe( 'Add your business name and a two-letter country in Settings to preview.' ); expect( missingSettings({ ...EMPTY_RULES, seller_name: 'Acme', country: 'U' }) ).not.toBeNull(); expect( missingSettings({ ...EMPTY_RULES, seller_name: ' Acme ', country: 'US' }) ).toBeNull(); }); it('counts the changes from the saved rules and the pages of a search', () => { expect(changedRuleCount(EMPTY_RULES, EMPTY_RULES)).toBe(0); const draft = withBrandMode( { ...EMPTY_RULES, seller_name: 'Acme' }, 'Alera', 'excluded' ); expect(changedRuleCount(EMPTY_RULES, draft)).toBe(2); expect( changedRuleCount(draft, withBrandMode(draft, 'Alera', 'parent')) ).toBe(2); expect(pageCount(0, 50)).toBe(1); expect(pageCount(101, 50)).toBe(3); }); });