Files
Investidor-V2/tests/frontend/organization-country-editor.test.mjs
T

166 lines
6.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { harness, props, text } from './helpers/investment-profile-editor.mjs';
const id = '20000000-0000-4000-8000-000000000001',
form = 'País da organização: SYNTHETIC master',
country = 'País de constituição da organização',
source = 'Fonte ou referência deste país';
const org = (patch = {}) => ({
id,
name: 'SYNTHETIC master',
version: 1,
country: 'MZ',
canEdit: true,
provenance: {
kind: 'representative_declaration',
reference: 'SYNTHETIC saved reference',
},
...patch,
});
const state = (organization = org(), patch = {}) =>
props({ organizations: [organization], ...patch });
test('organization country and source are frozen and repeated together after an uncertain write', async () => {
const h = harness(state());
h.change(country, 'ZA', form);
h.change(source, 'SYNTHETIC new source', form);
const first = h.submit(form);
assert.equal(first.method, 'PUT');
assert.equal(
first.url,
'/api/v1/investment/organizations/' + id + '/country',
);
assert.equal(first.data.country, 'ZA');
assert.equal(first.data.source_reference, 'SYNTHETIC new source');
assert.equal(first.data.payload, undefined);
await h.fail();
assert.equal(h.field(source, form).props.disabled, true);
assert.equal(h.value(country, form), 'ZA');
const retry = h.submit(form);
assert.deepEqual(retry.data, first.data);
await h.reply({ version: 2, country: 'ZA' });
assert.match(text(h.form(form)), /Gravação confirmada/);
assert.deepEqual(h.reloads[0].only, ['organizations']);
});
test('an organization version refresh preserves both local values and uses explicit comparison before another write', async () => {
const h = harness(state());
h.change(country, 'ZA', form);
h.change(source, 'SYNTHETIC local reference', form);
h.render(
state(
org({
version: 3,
country: 'PT',
provenance: {
kind: 'representative_declaration',
reference: 'SYNTHETIC remote source',
},
}),
),
);
assert.equal(h.value(source, form), 'SYNTHETIC local reference');
assert.equal(
h.button('Guardar país da organização', form).props.disabled,
true,
);
h.click('Consultar país guardado', form);
await h.reply(
state(
org({
version: 3,
country: 'PT',
provenance: {
kind: 'representative_declaration',
reference: 'SYNTHETIC remote source',
},
}),
),
);
assert.match(text(h.form(form)), /SYNTHETIC remote source/);
h.click('Usar versão actual e manter a preparação', form);
const next = h.submit(form);
assert.equal(next.data.version, 3);
assert.equal(next.data.country, 'ZA');
assert.equal(next.data.source_reference, 'SYNTHETIC local reference');
});
test('a validation error belongs to the organization field once and does not remove the reference', async () => {
const h = harness(state());
h.change(source, 'x', form);
const sent = h.submit(form);
await h.fail(422, sent, {
source_reference: ['SYNTHETIC source too short'],
});
assert.equal(h.value(source, form), 'x');
assert.equal(h.field(source, form).props['aria-invalid'], true);
assert.equal(
text(h.form(form)).split('SYNTHETIC source too short').length - 1,
1,
);
});
test('read-only representation has no write command and cannot send one by direct form submission', () => {
const h = harness(state(org({ canEdit: false })));
assert.equal(h.button('Guardar país da organização', form), undefined);
assert.equal(h.field(country, form).props.disabled, true);
h.submit(form);
assert.equal(h.sent.length, 0);
});
test('a lost write followed by revoked representation remains prepared but cannot be repeated', async () => {
const h = harness(state());
h.change(source, 'SYNTHETIC retained source', form);
h.submit(form);
await h.fail();
h.click('Consultar país guardado', form);
await h.reply(state(org({ canEdit: false })));
const count = h.sent.length;
h.submit(form);
assert.equal(h.sent.length, count);
assert.equal(h.value(source, form), 'SYNTHETIC retained source');
assert.equal(h.button('Repetir o mesmo pedido', form), undefined);
});
test('organization edits share one leave guard with the private profile', () => {
const h = harness(state());
h.change(source, 'SYNTHETIC dirty organization', form);
h.change('Telefone', 'SYNTHETIC dirty profile', 'Perfil de investimento');
assert.equal(h.leave(), true);
assert.equal(h.confirmCalls, 1);
});
test('an acknowledgement with the wrong country cannot confirm or erase the prepared source', async () => {
const h = harness(state());
h.change(country, 'ZA', form);
h.change(source, 'SYNTHETIC prepared source', form);
h.submit(form);
await h.reply({ version: 2, country: 'PT' });
assert.equal(h.reloads.length, 0);
assert.equal(h.value(source, form), 'SYNTHETIC prepared source');
assert.match(text(h.form(form)), /Não foi possível confirmar/);
});
test('two rapid consultations share one in-flight read and never write automatically', async () => {
const h = harness(state());
const read = h.button('Consultar país guardado', form);
read.props.onClick();
read.props.onClick();
h.render();
assert.equal(h.sent.length, 1);
assert.equal(h.sent[0].method, 'GET');
await h.reply(state());
assert.equal(h.sent.length, 1);
assert.equal(
h.button('Guardar país da organização', form).props.disabled,
true,
);
});
test('a fresh authorized read can restore editing after a membership is restored while old page props are read-only', async () => {
const h = harness(state(org({ canEdit: false })));
h.click('Consultar país guardado', form);
await h.reply(state(org({ canEdit: true })));
h.click('Usar versão actual e manter a preparação', form);
assert.equal(h.field(source, form).props.disabled, false);
h.change(source, 'SYNTHETIC restored representation source', form);
const sent = h.submit(form);
assert.equal(
sent.data.source_reference,
'SYNTHETIC restored representation source',
);
assert.equal(sent.method, 'PUT');
});