From 8b4f17fb34f29b14ff81b9588e2b53886a1855c7 Mon Sep 17 00:00:00 2001
From: Kyle Angelo Galendez <chessurisme@gmail.com>
Date: Thu, 1 Aug 2024 08:23:20 +0800
Subject: [PATCH] test: add more examples in validation pass

---
 .../__tests__/sanitize-value.test.js          | 61 +++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 src/utilities/__tests__/sanitize-value.test.js

diff --git a/src/utilities/__tests__/sanitize-value.test.js b/src/utilities/__tests__/sanitize-value.test.js
new file mode 100644
index 0000000..59bbc83
--- /dev/null
+++ b/src/utilities/__tests__/sanitize-value.test.js
@@ -0,0 +1,61 @@
+import { JSDOM } from 'jsdom'
+import { sanitizeValue } from '@utilities/sanitize-value'
+
+const dom = new JSDOM('<!DOCTYPE html>')
+global.window = dom.window
+global.document = window.document
+global.HTMLElement = window.HTMLElement
+global.MouseEvent = window.MouseEvent
+global.DocumentFragment = window.DocumentFragment
+
+describe('sanitizeValue', () => {
+	beforeEach(() => {
+		jest.spyOn(console, 'error').mockImplementation(() => {})
+	})
+
+	afterEach(() => {
+		console.error.mockRestore()
+	})
+
+	it('should return null and log an error when value is undefined or null', () => {
+		expect(sanitizeValue(undefined, 'string')).toBeNull()
+		expect(console.error).toHaveBeenCalledWith('Invalid input: value is required.')
+
+		expect(sanitizeValue(null, 'string')).toBeNull()
+		expect(console.error).toHaveBeenCalledWith('Invalid input: value is required.')
+	})
+
+	it('should return null and log an error when type is missing', () => {
+		expect(sanitizeValue('test')).toBeNull()
+		expect(console.error).toHaveBeenCalledWith('Invalid input: type is required.')
+	})
+
+	it('should return null and log an error when value is of an invalid type', () => {
+		expect(sanitizeValue(new Date(), 'string')).toBeNull()
+		expect(console.error).toHaveBeenCalledWith(
+			'Notice: The value and type are not equal. Value is of type object, while type is string.'
+		)
+	})
+
+	it('should return null and log an error when type is not a string', () => {
+		expect(sanitizeValue('test', 123)).toBeNull()
+		expect(console.error).toHaveBeenCalledWith('Invalid input: type must be a string.')
+	})
+
+	it('should return null and log an error when value does not match the specified type', () => {
+		expect(sanitizeValue('test', 'number')).toBeNull()
+		expect(console.error).toHaveBeenCalledWith(
+			'Notice: The value and type are not equal. Value is of type string, while type is number.'
+		)
+	})
+
+	it('should return the value if all validations pass', () => {
+		expect(sanitizeValue('test', 'string')).toBe('test')
+		expect(sanitizeValue(123, 'number')).toBe(123)
+		expect(sanitizeValue([], 'array')).toEqual([])
+		expect(sanitizeValue({}, 'object')).toEqual({})
+		expect(sanitizeValue(false, 'boolean')).toBe(false)
+		expect(sanitizeValue(document.createElement('div'), 'HTMLElement')).toBeInstanceOf(HTMLElement)
+		expect(sanitizeValue(null, 'null')).toBeNull()
+	})
+})