Skip to content

Commit

Permalink
add unit tests for import onyx state
Browse files Browse the repository at this point in the history
  • Loading branch information
TMisiukiewicz committed Dec 13, 2024
1 parent fde7037 commit 531b937
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/unit/ImportOnyxStateTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {cleanAndTransformState, transformNumericKeysToArray} from '@components/ImportOnyxState/utils';
import ONYXKEYS from '@src/ONYXKEYS';

describe('transformNumericKeysToArray', () => {
it('converts object with numeric keys to array', () => {
const input = {'0': 'a', '1': 'b', '2': 'c'};
expect(transformNumericKeysToArray(input)).toEqual(['a', 'b', 'c']);
});

it('handles nested numeric objects', () => {
const input = {
'0': {'0': 'a', '1': 'b'},
'1': {'0': 'c', '1': 'd'},
};
expect(transformNumericKeysToArray(input)).toEqual([
['a', 'b'],
['c', 'd'],
]);
});

it('preserves non-numeric keys', () => {
const input = {foo: 'bar', baz: {'0': 'qux'}};
expect(transformNumericKeysToArray(input)).toEqual({foo: 'bar', baz: ['qux']});
});

it('handles empty objects', () => {
expect(transformNumericKeysToArray({})).toEqual({});
});

it('handles non-sequential numeric keys', () => {
const input = {'0': 'a', '2': 'b', '5': 'c'};
expect(transformNumericKeysToArray(input)).toEqual({'0': 'a', '2': 'b', '5': 'c'});
});
});

describe('cleanAndTransformState', () => {
it('removes omitted keys and transforms numeric objects', () => {
const input = JSON.stringify({
[ONYXKEYS.NETWORK]: 'should be removed',
someKey: {'0': 'a', '1': 'b'},
otherKey: 'value',
});

expect(cleanAndTransformState(input)).toEqual({
someKey: ['a', 'b'],
otherKey: 'value',
});
});

it('handles empty state', () => {
expect(cleanAndTransformState('{}')).toEqual({});
});

it('removes keys that start with omitted keys', () => {
const input = JSON.stringify({
[`${ONYXKEYS.NETWORK}_something`]: 'should be removed',
validKey: 'keep this',
});

expect(cleanAndTransformState(input)).toEqual({
validKey: 'keep this',
});
});

it('throws on invalid JSON', () => {
expect(() => cleanAndTransformState('invalid json')).toThrow();
});

it('removes all specified ONYXKEYS', () => {
const input = JSON.stringify({
[ONYXKEYS.ACTIVE_CLIENTS]: 'remove1',
[ONYXKEYS.FREQUENTLY_USED_EMOJIS]: 'remove2',
[ONYXKEYS.NETWORK]: 'remove3',
[ONYXKEYS.CREDENTIALS]: 'remove4',
[ONYXKEYS.PREFERRED_THEME]: 'remove5',
keepThis: 'value',
});

const result = cleanAndTransformState(input);

expect(result).toEqual({
keepThis: 'value',
});

// Verify each key is removed
expect(result).not.toHaveProperty(ONYXKEYS.ACTIVE_CLIENTS);
expect(result).not.toHaveProperty(ONYXKEYS.FREQUENTLY_USED_EMOJIS);
expect(result).not.toHaveProperty(ONYXKEYS.NETWORK);
expect(result).not.toHaveProperty(ONYXKEYS.CREDENTIALS);
expect(result).not.toHaveProperty(ONYXKEYS.PREFERRED_THEME);
});
});

0 comments on commit 531b937

Please sign in to comment.