Skip to content

Commit

Permalink
Implemented tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmathis committed Jun 16, 2021
1 parent 2e60252 commit a9deae8
Show file tree
Hide file tree
Showing 34 changed files with 2,206 additions and 2,089 deletions.
3,536 changes: 1,559 additions & 1,977 deletions ui/package-lock.json

Large diffs are not rendered by default.

44 changes: 23 additions & 21 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"@testing-library/jest-dom": "^5.13.0",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.1.10",
"fast-check": "^2.16.0",
"http-proxy-middleware": "^1.2.0",
"jest-fast-check": "^1.0.2",
"react-scripts": "4.0.3",
"sass": "1.32.11"
},
Expand Down Expand Up @@ -73,47 +75,47 @@
"lines": 80,
"statements": 80
},
"./src/app/metadata/domain/": {
"./src/app/metadata/domain/filter/definition": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
},
"./src/app/metadata/domain/filter/component/": {
"branches": 0,
"functions": 0,
"lines": 0,
"statements": 0
},
"./src/app/metadata/domain/source/component/": {
"branches": 0,
"functions": 0,
"lines": 0,
"statements": 0
},
"./src/app/metadata/domain/provider/component/": {
"branches": 0,
"functions": 0,
"lines": 0,
"statements": 0
"./src/app/metadata/domain/provider/definition/": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
},
"./src/app/metadata/hooks/": {
"./src/app/metadata/domain/source/definition/": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
},
"./src/app/metadata/contention/": {
"./src/app/metadata/domain/attribute/": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
},
"./src/app/metadata/component/": {
"./src/app/metadata/hooks/": {
"branches": 50,
"functions": 80,
"lines": 80,
"statements": 80
},
"./src/app/metadata/contention/": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
},
"./src/app/metadata/component/": {
"branches": 0,
"functions": 0,
"lines": 0,
"statements": 0
}
}
},
Expand Down
10 changes: 5 additions & 5 deletions ui/src/app/metadata/contention/ContentionContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ const keys = [
'@type'
];

const filterKeys = (key => (keys.indexOf(key) === -1));
export const filterKeys = (key => (keys.indexOf(key) === -1));


const getContention = (base, ours, theirs) => {
export const getContention = (base, ours, theirs) => {

let theirDiff = updatedDiff(base, theirs);
let ourDiff = updatedDiff(base, removeNull(ours));
Expand All @@ -59,7 +59,7 @@ const getContention = (base, ours, theirs) => {
};
}

const getChangeItem = (key, collection, compare = []) => {
export const getChangeItem = (key, collection, compare = []) => {
return {
label: key,
value: collection[key],
Expand Down Expand Up @@ -93,7 +93,7 @@ export const resolveContentionAction = () => {
}
}

function reducer(state, action) {
export function reducer(state, action) {
switch (action.type) {
case ContentionActions.OPEN_CONTENTION_MODAL:
return {
Expand All @@ -106,7 +106,7 @@ function reducer(state, action) {
show: false
};
default:
throw new Error();
return state;
}
}

Expand Down
67 changes: 67 additions & 0 deletions ui/src/app/metadata/contention/ContentionContext.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { render, screen } from "@testing-library/react";
import { Contention, ContentionActions, filterKeys, getContention, openContentionModalAction, reducer, resolveContentionAction } from "./ContentionContext";

jest.mock('../../i18n/hooks', () => ({
useTranslator: () => (value) => value,
useTranslation: (value) => value
}));

describe('filterKeys', () => {
it('should filter keys that arent displayed to user', () => {
expect(filterKeys({
version: 'foo',
name: 'bar'
})).toBe(true)
})
});

describe('getContention', () => {
it('should return an object describing conflicts', () => {
const contention = getContention({ name: 'baz', version: 0 }, { name: 'foo', version: 1 }, { name: 'bar', version: 2 });
expect(contention.theirChanges[0].conflict).toBe(true);
});
});

describe('openContentionModalAction', () => {
it('should create a reducer action', () => {
const action = openContentionModalAction({}, {name: 'foo'}, {name: 'bar'}, jest.fn(), jest.fn());
expect(action.type).toEqual(ContentionActions.OPEN_CONTENTION_MODAL);
})
});

describe('resolveContentionAction', () => {
it('should create a reducer action', () => {
const action = resolveContentionAction();
expect(action.type).toEqual(ContentionActions.END_CONTENTION);
})
});

describe('reducer', () => {
it('should set show to true', () => {
const state = reducer({}, {type: ContentionActions.OPEN_CONTENTION_MODAL});
expect(state.show).toBe(true);
})

it('should set show to false', () => {
const state = reducer({}, { type: ContentionActions.END_CONTENTION });
expect(state.show).toBe(false);
})

it('should not change state', () => {
const init = {};
const state = reducer(init, { type: 'foo' });
expect(state).toBe(init);
})
});

describe('context component', () => {

it('should provide the contention context', async () => {
render(<Contention>
<div>test</div>
</Contention>);

expect(screen.getByText('test')).toBeInTheDocument();
});

})
8 changes: 2 additions & 6 deletions ui/src/app/metadata/contention/component/ChangeItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export function ChangeItem ({item}) {
<thead>
<tr>
{display.headings.map((heading, idx) =>
<th>
<Translate key={heading}>{heading}</Translate>
<th key={idx}>
<Translate value={heading}>{heading}</Translate>
</th>
)}
</tr>
Expand All @@ -103,12 +103,8 @@ export function ChangeItem ({item}) {
<td>{val}</td>
</tr>
)}

</tbody>
}



</table>
:
type === ValueTypes.object ?
Expand Down
62 changes: 62 additions & 0 deletions ui/src/app/metadata/contention/component/ChangeItem.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';

import { ChangeItem } from './ChangeItem';

jest.mock('../../../i18n/hooks', () => ({
useTranslator: () => (value) => value,
useTranslation: (value) => value
}));

describe('Contention Modal', () => {

it('should render strings', () => {
render(<ChangeItem
item={{
label: 'foo',
value: 'bar',
conflict: true
}} />);
expect(screen.getByText('foo')).toBeInTheDocument();
expect(screen.getByText('bar')).toBeInTheDocument();
});

it('should render objects', () => {
const item = {
label: 'foo',
value: {
bar: 'baz'
}
};
render(<ChangeItem
item={item} />);
expect(screen.getByText('baz')).toBeInTheDocument();
});

it('should render arrays of primitives', () => {
const item = {
label: 'foo',
value: [
'bar',
'baz'
]
};
render(<ChangeItem
item={item} />);
expect(screen.getByText('baz')).toBeInTheDocument();
});

it('should render arrays of objects', () => {
const item = {
label: 'foo',
value: [
{ barlabel: 'bar' },
{ bazlabel: 'baz' }
]
};
render(<ChangeItem
item={item} />);
expect(screen.getByText('baz')).toBeInTheDocument();
expect(screen.getByText('bar')).toBeInTheDocument();
});
});
9 changes: 0 additions & 9 deletions ui/src/app/metadata/contention/component/ContentionModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ export function ContentionModal ({ theirs = [], ours = [], onUseTheirs, onUseOur
<h4 className="modal-title"><Translate value="message.data-version-contention">Data Version Contention</Translate></h4>
</Modal.Header>
<Modal.Body>
{/*<div className="row">
<div className="col-6">
<pre>{JSON.stringify(theirs, null, 4)}</pre>
</div>
<div className="col-6">
<pre>{JSON.stringify(ours, null, 4)}</pre>
</div>
</div>*/}

<div className="d-flex w-50 justify-content-center mb-4 mx-auto">
<FontAwesomeIcon icon={faExclamationTriangle} size="3x" className="text-warning" />
{theirs && theirs.length > 0 ?
Expand Down
58 changes: 58 additions & 0 deletions ui/src/app/metadata/contention/component/ContentionModal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';

import { ContentionModal } from './ContentionModal';
import { getContention } from '../ContentionContext';

jest.mock('../../../i18n/hooks', () => ({
useTranslator: () => (value) => value,
useTranslation: (value) => value
}));


describe('Contention Modal', () => {

let contention, mockUseTheirs, mockUseOurs;

beforeEach(() => {
mockUseOurs = jest.fn();
mockUseTheirs = jest.fn();
contention = getContention({ name: 'baz', version: 0 }, { name: 'foo', version: 1 }, { name: 'bar', version: 2 });
render(<ContentionModal
show={true}
theirs={contention.theirChanges}
ours={contention.ourChanges}
onUseOurs={mockUseOurs}
onUseTheirs={mockUseTheirs} />);
})


it('should render', () => {
expect(screen.getByText('message.data-version-contention')).toBeInTheDocument();
expect(screen.getByText('message.contention-new-version')).toBeInTheDocument();
});

it('should allow the user to discard their changes', () => {
fireEvent.click(screen.getByText('action.use-theirs'));
expect(mockUseTheirs).toHaveBeenCalled();
});

it('should allow the user to resolve differences', () => {
fireEvent.click(screen.getByText('action.use-mine'));
expect(mockUseOurs).toHaveBeenCalled();
});

it('should allow the user to cancel if there are no real changes', () => {

contention = getContention({ name: 'baz', version: 0 }, { name: 'foo', version: 1 }, { name: 'bar', version: 2 });
render(<ContentionModal
show={true}
theirs={[]}
ours={contention.ourChanges}
onUseOurs={mockUseOurs}
onUseTheirs={mockUseTheirs} />);

fireEvent.click(screen.getByText('action.cancel'));
expect(mockUseTheirs).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import API_BASE_PATH from "../../../App.constant";
import API_BASE_PATH from "../../../../App.constant";
import {BaseFilterDefinition} from './BaseFilterDefinition';
import {removeNull} from '../../../core/utility/remove_null';
import { isValidRegex } from '../../../core/utility/is_valid_regex';
import {removeNull} from '../../../../core/utility/remove_null';
import { isValidRegex } from '../../../../core/utility/is_valid_regex';
import defaultsDeep from "lodash/defaultsDeep";

export const EntityAttributesFilterWizard = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import defaultsDeep from "lodash/defaultsDeep";
import API_BASE_PATH from "../../../App.constant";
import API_BASE_PATH from "../../../../App.constant";
import { BaseFilterDefinition } from "./BaseFilterDefinition";

import { isValidRegex } from '../../../core/utility/is_valid_regex';
import { isValidRegex } from '../../../../core/utility/is_valid_regex';

export const NameIDFilterWizard = {
...BaseFilterDefinition,
Expand Down
4 changes: 2 additions & 2 deletions ui/src/app/metadata/domain/filter/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EntityAttributesFilterEditor } from './EntityAttributesFilterDefinition';
import { NameIDFilterEditor } from './NameIdFilterDefinition';
import { EntityAttributesFilterEditor } from './definition/EntityAttributesFilterDefinition';
import { NameIDFilterEditor } from './definition/NameIdFilterDefinition';

export const MetadataFilterWizardTypes = {
EntityAttributes: EntityAttributesFilterEditor,
Expand Down
8 changes: 4 additions & 4 deletions ui/src/app/metadata/domain/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CustomAttributeDefinition } from './attribute/CustomAttributeDefinition';
import { MetadataFilterEditorTypes } from './filter';
import { MetadataProviderEditorTypes, MetadataProviderWizardTypes } from './provider';
import { DynamicHttpMetadataProviderEditor } from './provider/DynamicHttpMetadataProviderDefinition';
import { FileBackedHttpMetadataProviderEditor } from './provider/FileBackedHttpMetadataProviderDefinition';
import { LocalDynamicMetadataProviderEditor } from './provider/LocalDynamicMetadataProviderDefinition';
import { SourceEditor, SourceWizard } from "./source/SourceDefinition";
import { DynamicHttpMetadataProviderEditor } from './provider/definition/DynamicHttpMetadataProviderDefinition';
import { FileBackedHttpMetadataProviderEditor } from './provider/definition/FileBackedHttpMetadataProviderDefinition';
import { LocalDynamicMetadataProviderEditor } from './provider/definition/LocalDynamicMetadataProviderDefinition';
import { SourceEditor, SourceWizard } from "./source/definition/SourceDefinition";

export const editors = {
source: SourceEditor
Expand Down
Loading

0 comments on commit a9deae8

Please sign in to comment.