123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import React from 'react';
- import {mount} from 'enzyme';
- import Annotated from 'app/components/events/meta/annotated';
- import {withMeta} from 'app/components/events/meta/metaProxy';
- describe('Annotated', () => {
- const mock = jest.fn(() => null);
- const createEvent = (value, {err, rem, chunks} = {}) => {
- return withMeta({
- value,
- _meta: {
- value: {
- '': {
- err: err || [],
- rem: rem || [],
- chunks: chunks || [],
- },
- },
- },
- });
- };
- beforeEach(function() {
- mock.mockClear();
- });
- describe('without meta', () => {
- it('renders a string', () => {
- const obj = {
- value: 'foo',
- };
- mount(
- <Annotated object={obj} prop="value">
- {mock}
- </Annotated>
- );
- expect(mock).toHaveBeenCalledWith('foo');
- });
- it('does not error if prop does not exist on object', () => {
- const obj = {
- value: 'foo',
- };
- mount(<Annotated object={obj} prop="invalid" />);
- });
- it('renders a number', () => {
- const obj = {
- value: 0,
- };
- mount(
- <Annotated object={obj} prop="value">
- {mock}
- </Annotated>
- );
- expect(mock).toHaveBeenCalledWith(0);
- });
- it('renders a boolean', () => {
- const obj = {
- value: false,
- };
- mount(
- <Annotated object={obj} prop="value">
- {mock}
- </Annotated>
- );
- expect(mock).toHaveBeenCalledWith(false);
- });
- it('ignores empty meta data', () => {
- const obj = withMeta({
- value: 'foo',
- _meta: {
- value: {
- '': {
- err: [],
- rem: [],
- chunks: [],
- },
- },
- },
- });
- mount(
- <Annotated object={obj} prop="value">
- {mock}
- </Annotated>
- );
- expect(mock).toHaveBeenCalledWith('foo');
- });
- it('does not call render prop if required and value is falsy and no meta', () => {
- const obj = createEvent(null, {});
- mount(
- <Annotated object={obj} prop="value" required>
- {mock}
- </Annotated>
- );
- expect(mock).not.toHaveBeenCalled();
- });
- });
- describe('with meta', () => {
- it('annotates errors', () => {
- const obj = createEvent('foo', {err: ['something']});
- mount(
- <Annotated object={obj} prop="value">
- {mock}
- </Annotated>
- );
- expect(mock.mock.calls[0][0].props).toEqual(
- expect.objectContaining({
- value: 'foo',
- chunks: [],
- remarks: [],
- errors: ['something'],
- })
- );
- });
- it('annotates remarks and chunks', () => {
- const obj = createEvent('foo', {rem: [{type: 't'}], chunks: [{text: 'foo'}]});
- mount(
- <Annotated object={obj} prop="value">
- {mock}
- </Annotated>
- );
- expect(mock.mock.calls[0][0].props).toEqual(
- expect.objectContaining({
- value: 'foo',
- remarks: [{type: 't'}],
- chunks: [{text: 'foo'}],
- errors: [],
- })
- );
- });
- it('annotates redacted text', () => {
- const obj = createEvent(null, {err: ['something']});
- mount(
- <Annotated object={obj} prop="value">
- {mock}
- </Annotated>
- );
- expect(mock.mock.calls[0][0].props).toEqual(
- expect.objectContaining({
- value: null,
- chunks: [],
- remarks: [],
- errors: ['something'],
- })
- );
- });
- });
- });
|