51 lines
2.8 KiB
TypeScript
51 lines
2.8 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { EMPTY_VALUES } from './leadFormModel';
|
|
import { applyPrefill, getLeadPrefill, prefillLead, subscribeLeadPrefill } from './leadPrefill';
|
|
|
|
const ethiopia = { interest: 'one_time', comment: 'Эфиопия Иргачеффе, 250 г' } as const;
|
|
|
|
describe('applyPrefill', () => {
|
|
it('ставит интерес и комментарий в пустую форму', () => {
|
|
const r = applyPrefill(EMPTY_VALUES, ethiopia, null);
|
|
expect(r.values).toMatchObject({ interest: 'one_time', comment: 'Эфиопия Иргачеффе, 250 г' });
|
|
expect(r.prefilledComment).toBe('Эфиопия Иргачеффе, 250 г');
|
|
});
|
|
|
|
it('не затирает комментарий, который посетитель напечатал сам', () => {
|
|
const values = { ...EMPTY_VALUES, interest: 'question' as const, comment: 'Можно помолоть под турку?' };
|
|
const r = applyPrefill(values, ethiopia, null);
|
|
expect(r.values).toMatchObject({ interest: 'one_time', comment: 'Можно помолоть под турку?' });
|
|
});
|
|
|
|
it('заменяет свой прошлый предзаполненный комментарий', () => {
|
|
const first = applyPrefill(EMPTY_VALUES, ethiopia, null);
|
|
const second = applyPrefill(first.values, { interest: 'subscription' }, first.prefilledComment);
|
|
expect(second.values).toMatchObject({ interest: 'subscription', comment: '' });
|
|
expect(second.prefilledComment).toBeNull();
|
|
});
|
|
|
|
it('если посетитель дописал предзаполненный комментарий — он уже его', () => {
|
|
const first = applyPrefill(EMPTY_VALUES, ethiopia, null);
|
|
const edited = { ...first.values, comment: `${first.values.comment}, помол под эспрессо` };
|
|
const second = applyPrefill(edited, { interest: 'subscription' }, first.prefilledComment);
|
|
expect(second.values.comment).toBe('Эфиопия Иргачеффе, 250 г, помол под эспрессо');
|
|
});
|
|
|
|
it('пробелы в поле не считаются текстом посетителя', () => {
|
|
const r = applyPrefill({ ...EMPTY_VALUES, comment: ' ' }, ethiopia, null);
|
|
expect(r.values.comment).toBe('Эфиопия Иргачеффе, 250 г');
|
|
});
|
|
});
|
|
|
|
describe('prefillLead', () => {
|
|
it('уведомляет подписчиков и работает без DOM', () => {
|
|
const got: unknown[] = [];
|
|
const unsubscribe = subscribeLeadPrefill((p) => got.push(p));
|
|
prefillLead({ interest: 'question', source: 'hero' });
|
|
unsubscribe();
|
|
prefillLead({ interest: 'subscription' });
|
|
expect(got).toEqual([expect.objectContaining({ interest: 'question', source: 'hero' })]);
|
|
expect(getLeadPrefill()).toMatchObject({ interest: 'subscription' });
|
|
});
|
|
});
|