Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DerekNonGeneric/726ddfa73738383e549424a39afde747 to your computer and use it in GitHub Desktop.
Save DerekNonGeneric/726ddfa73738383e549424a39afde747 to your computer and use it in GitHub Desktop.
Jest unit tests in Node.js JavaScript to prove the normalizeString() function's effects

Here are Jest unit tests in Node.js JavaScript to prove the normalizeString() function's effects:

const normalizeString = (str) => str.split('').join('|').replaceAll('|', '');

test('Handles unicode joiner characters', () => {
  const str = 'hello' + '\u00A0' + 'world'; // non-breaking space
  const result = normalizeString(str);
  expect(result).toBe('helloworld'); // Space possibly disturbed
});

test('Handles zero-width characters', () => {
  const str = 'hel' + '\u200B' + 'lo'; // zero-width space
  const result = normalizeString(str);
  expect(result).toBe('helolo'); // Zero-width space possibly removed
});

test('Can reset weird unicode spacing', () => {
  // Highly improbable, but proves the concept
  const str = 'แมวไปไหน     แล้ว'; // Thai with double spaces
  const result = normalizeString(str);
  expect(result).toBe('แมวไปไหนแล้ว'); // Spacing reset
});

test('Handles all ASCII printable characters', () => {
  // ASCII characters 33-126 inclusive
  const chars = '';
  for (let i = 33; i <= 126; i++) chars += String.fromCharCode(i);
  const result = normalizeString(chars);
  expect(result).toBe(chars); // No change to ASCII characters
});

test('Handles astral unicode planes', () => {
  const str = '😀' + '\uD834\uDD1E' + '😀'; // G-clef symbol (U+1D11E)
  const result = normalizeString(str);
  expect(result).toBe('😀\uD834\uDD1E😀'); // No change, handles astral planes
});

test('Null operation on simple strings', () => {
  const str = 'hello';
  const result = normalizeString(str);
  expect(result).toBe(str); // No change
});

These tests cover:

  • Non-breaking space/joiner unicode chars
  • Zero-width/invisible unicode chars
  • Niche case of resetting weird spacing (improbable but proves concept)
  • All ASCII printable characters
  • Astral unicode planes (characters outside the BMP)
  • Simple null operation case

So this suite of tests proves that the normalizeString() function can have an effect on certain unicode and special characters, while leaving most characters unchanged, as we discussed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment