From f30f4ecf0e29dd7acfb04534bc06ea00f2a22946 Mon Sep 17 00:00:00 2001 From: Ryan Mathis Date: Wed, 9 Jun 2021 07:01:44 -0700 Subject: [PATCH] Unit tests --- ui/src/app/core/utility/array_move.test.js | 9 ++++ .../app/core/utility/download_as_xml.test.js | 11 +++++ ui/src/app/core/utility/get_cookie.test.js | 10 +++++ .../app/core/utility/is_valid_regex.test.js | 9 ++++ ui/src/app/core/utility/remove_null.test.js | 14 ++++++ ui/src/app/core/utility/uuid.test.js | 5 +++ .../app/metadata/hoc/MetadataSchema.test.js | 44 +++++++++++++++++++ ui/src/app/metadata/hooks/api.test.js | 0 ui/src/app/metadata/hooks/schema.test.js | 0 ui/src/app/metadata/hooks/utility.test.js | 0 10 files changed, 102 insertions(+) create mode 100644 ui/src/app/core/utility/array_move.test.js create mode 100644 ui/src/app/core/utility/download_as_xml.test.js create mode 100644 ui/src/app/core/utility/get_cookie.test.js create mode 100644 ui/src/app/core/utility/is_valid_regex.test.js create mode 100644 ui/src/app/core/utility/remove_null.test.js create mode 100644 ui/src/app/core/utility/uuid.test.js create mode 100644 ui/src/app/metadata/hoc/MetadataSchema.test.js create mode 100644 ui/src/app/metadata/hooks/api.test.js create mode 100644 ui/src/app/metadata/hooks/schema.test.js create mode 100644 ui/src/app/metadata/hooks/utility.test.js diff --git a/ui/src/app/core/utility/array_move.test.js b/ui/src/app/core/utility/array_move.test.js new file mode 100644 index 000000000..f8770970e --- /dev/null +++ b/ui/src/app/core/utility/array_move.test.js @@ -0,0 +1,9 @@ +import {array_move} from './array_move'; + +it('shifts an item in an array by +1 index', () => { + expect(array_move([1, 2], 0, 1)).toEqual([2, 1]); +}); + +it('shifts an item in an array by -1 index', () => { + expect(array_move([1, 2], 1, 0)).toEqual([2, 1]); +}); \ No newline at end of file diff --git a/ui/src/app/core/utility/download_as_xml.test.js b/ui/src/app/core/utility/download_as_xml.test.js new file mode 100644 index 000000000..38a87e6fe --- /dev/null +++ b/ui/src/app/core/utility/download_as_xml.test.js @@ -0,0 +1,11 @@ +import * as FileSaver from 'file-saver'; +import { downloadAsXml } from './download_as_xml'; +jest.mock('file-saver'); + +it('attempts to save the provided content', () => { + const name = 'foo.xml'; + const xml = ''; + + downloadAsXml(name, xml); + expect(FileSaver.saveAs).toHaveBeenCalled(); +}); diff --git a/ui/src/app/core/utility/get_cookie.test.js b/ui/src/app/core/utility/get_cookie.test.js new file mode 100644 index 000000000..83d09bfe5 --- /dev/null +++ b/ui/src/app/core/utility/get_cookie.test.js @@ -0,0 +1,10 @@ +import { get_cookie } from './get_cookie'; + +Object.defineProperty(window.document, 'cookie', { + writable: true, + value: 'XSRF=FOOBAR', +}); + +it('should retrieve the XSRF Cookie', () => { + expect(get_cookie('XSRF')).toEqual('FOOBAR'); +}); \ No newline at end of file diff --git a/ui/src/app/core/utility/is_valid_regex.test.js b/ui/src/app/core/utility/is_valid_regex.test.js new file mode 100644 index 000000000..7365b5818 --- /dev/null +++ b/ui/src/app/core/utility/is_valid_regex.test.js @@ -0,0 +1,9 @@ +import { isValidRegex } from './is_valid_regex'; + +it('should return false for a malformed regular expression', () => { + expect(isValidRegex(`\\f;klsdflk;sdf()**(&*&^^()`)).toBe(false) +}); + +it('should return true for a well-formed regular expression', () => { + expect(isValidRegex(`[a-z0-9][a-z0-9-]{0,31}:`)).toBe(true) +}); \ No newline at end of file diff --git a/ui/src/app/core/utility/remove_null.test.js b/ui/src/app/core/utility/remove_null.test.js new file mode 100644 index 000000000..490f64abb --- /dev/null +++ b/ui/src/app/core/utility/remove_null.test.js @@ -0,0 +1,14 @@ +import { removeNull } from './remove_null'; + +it('should remove null values from an object', () => { + + const obj = { + foo: null, + bar: { + baz: null + } + }; + + expect(removeNull(obj, true)).toEqual({}); + expect(removeNull(obj, false)).toEqual({bar: { baz: null }}); +}); \ No newline at end of file diff --git a/ui/src/app/core/utility/uuid.test.js b/ui/src/app/core/utility/uuid.test.js new file mode 100644 index 000000000..a9d0312c2 --- /dev/null +++ b/ui/src/app/core/utility/uuid.test.js @@ -0,0 +1,5 @@ +import { uuid } from './uuid'; + +it('should return a valid uuid', () => { + expect(uuid()).toMatch(/[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/); +}); \ No newline at end of file diff --git a/ui/src/app/metadata/hoc/MetadataSchema.test.js b/ui/src/app/metadata/hoc/MetadataSchema.test.js new file mode 100644 index 000000000..e8164a2cc --- /dev/null +++ b/ui/src/app/metadata/hoc/MetadataSchema.test.js @@ -0,0 +1,44 @@ +import React from "react"; +import ReactDOM from 'react-dom'; +import { render, screen } from "@testing-library/react"; +import { act } from 'react-dom/test-utils'; +import { MetadataSchema, useMetadataDefinitionContext } from './MetadataSchema'; + +let container; +beforeEach(() => { + container = document.createElement('div'); + document.body.appendChild(container); +}); + +afterEach(() => { + document.body.removeChild(container); + container = null; +}); + +const Tester = () => { + const definition = useMetadataDefinitionContext(); + + return ( + <> + <>{definition.type} + + ); +} + + +xdescribe("", () => { + /*xact(() => { + ReactDOM.render( + + + , + container + ); + });*/ + + xdescribe("definition context", () => { + it("should provide the type of the definition", () => { + expect(screen.getByText('@MetadataProvider')).toBeTruthy(); + }); + }); +}); \ No newline at end of file diff --git a/ui/src/app/metadata/hooks/api.test.js b/ui/src/app/metadata/hooks/api.test.js new file mode 100644 index 000000000..e69de29bb diff --git a/ui/src/app/metadata/hooks/schema.test.js b/ui/src/app/metadata/hooks/schema.test.js new file mode 100644 index 000000000..e69de29bb diff --git a/ui/src/app/metadata/hooks/utility.test.js b/ui/src/app/metadata/hooks/utility.test.js new file mode 100644 index 000000000..e69de29bb