Skip to content

Commit

Permalink
added renderer unit and integration tests. #1662
Browse files Browse the repository at this point in the history
  • Loading branch information
davemfish committed Jan 28, 2025
1 parent 24ca23e commit 5052e9f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function FormRow(label, value, handler) {
<Col sm="8">
<Form.Control
type="text"
value={value}
value={value || ''}
onChange={(e) => handler(e.currentTarget.value)}
/>
</Col>
Expand Down
19 changes: 18 additions & 1 deletion workbench/tests/renderer/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
getSpec,
fetchValidation,
fetchDatastackFromFile,
getSupportedLanguages
getSupportedLanguages,
getGeoMetaMakerProfile,
} from '../../src/renderer/server_requests';
import InvestJob from '../../src/renderer/InvestJob';
import {
Expand Down Expand Up @@ -377,6 +378,8 @@ describe('Display recently executed InVEST jobs on Home tab', () => {
});

test('Recent Jobs: cleared by button', async () => {
// we need this mock because the settings dialog is opened
getGeoMetaMakerProfile.mockResolvedValue({});
const job1 = new InvestJob({
modelRunName: MOCK_MODEL_RUN_NAME,
modelHumanName: 'Carbon Sequestration',
Expand Down Expand Up @@ -419,6 +422,7 @@ describe('InVEST global settings: dialog interactions', () => {
beforeEach(async () => {
getInvestModelNames.mockResolvedValue({});
getSupportedLanguages.mockResolvedValue({ en: 'english', es: 'spanish' });
getGeoMetaMakerProfile.mockResolvedValue({});
});

test('Invest settings save on change', async () => {
Expand Down Expand Up @@ -477,4 +481,17 @@ describe('InVEST global settings: dialog interactions', () => {
.toBeInTheDocument();
expect(queryByText('Settings')).toBeNull();
});

test('Access metadata form from settings', async () => {
const { findByRole } = render(<App />);

const settingsBtn = await findByRole('button', { name: 'settings' });
await userEvent.click(settingsBtn);
await userEvent.click(
await findByRole('button', { name: 'Configure Metadata' })
);

expect(await findByRole('button', { name: 'Save Metadata' }))
.toBeInTheDocument();
});
});
63 changes: 63 additions & 0 deletions workbench/tests/renderer/metadataform.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';

import MetadataForm from '../../src/renderer/components/SettingsModal/MetadataForm';
import {
getGeoMetaMakerProfile,
setGeoMetaMakerProfile,
} from '../../src/renderer/server_requests';

jest.mock('../../src/renderer/server_requests');

test('Metadata form interact and submit', async () => {
const startingName = 'Alice';
const startingLicense = '';
getGeoMetaMakerProfile.mockResolvedValue({
contact: {
individual_name: startingName,
email: '',
organization: '',
position_name: '',
},
license: {
title: startingLicense,
path: '',
},
});
setGeoMetaMakerProfile.mockResolvedValue({
message: 'Metadata profile saved',
error: false,
});

const user = userEvent.setup();
const { findByRole, getByLabelText, getByRole } = render(
<MetadataForm />
);
// The form should render with content from an existing profile
const nameInput = getByLabelText('Full name');
await waitFor(() => {
expect(nameInput).toHaveValue(startingName);
});
const licenseInput = getByLabelText('Title');
await waitFor(() => {
expect(licenseInput).toHaveValue(startingLicense);
});

const name = 'Bob';
const license = 'CC-BY-4.0';
await user.clear(nameInput);
await user.clear(licenseInput);
await user.type(nameInput, name);
await user.type(licenseInput, license);
const submit = getByRole('button', { name: 'Save Metadata' });
await user.click(submit);

expect(await findByRole('alert'))
.toHaveTextContent('Metadata profile saved');
const payload = setGeoMetaMakerProfile.mock.calls[0][0];
expect(Object.keys(payload)).toEqual(['contact', 'license']);
expect(payload['contact']['individual_name']).toEqual(name);
expect(payload['license']['title']).toEqual(license);
});

0 comments on commit 5052e9f

Please sign in to comment.