Skip to content

Commit

Permalink
Add tests for cases with arrays. Closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
vcgtz committed Apr 28, 2023
1 parent 71ae508 commit 46356ba
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion tests/ConfigurationStorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, describe, test, beforeEach, afterAll } from '@jest/globals';
import { expect, describe, test, beforeEach, afterAll, afterEach } from '@jest/globals';
import ConfigurationStorage from '../src/ConfigurationStorage';
import fsPromises from 'fs/promises';

Expand Down Expand Up @@ -39,6 +39,10 @@ describe('Testing folder name parsing', () => {
expect(storagePrototype.isValidKey('.hello.world.fellows.')).toBe(false);
});

afterEach(async () => {
await storage.clean();
});

afterAll(async () => {
await fsPromises.unlink(storage.path);
});
Expand Down Expand Up @@ -160,6 +164,69 @@ describe('Testing storing and retrieving data', () => {
expect(await storage.get('message.nestedMessage2', 666)).toBe(666);
});

afterEach(async () => {
await storage.clean();
});

afterAll(async () => {
await fsPromises.unlink(storage.path);
});
});

describe('Testing array storage', () => {
let storage: ConfigurationStorage;

beforeEach(async () => {
storage = await ConfigurationStorage.getStorage('testing');
});

test('Storing an array of numbers', async () => {
const key: string = 'my_array';
const value: number[] = [1, 2, 3, 4, 5];
const expectedObj: any = {
my_array: value,
};

await storage.set(key, value);

expect(await storage.getAll()).toMatchObject(expectedObj);
});

test('Storing a nested array of numbers', async () => {
const key: string = 'my_array.my_nested_array';
const value: number[] = [1, 2, 3, 4, 5];
const expectedObj: any = {
my_array: {
my_nested_array: value,
},
};

await storage.set(key, value);
console.log(await storage.getAll());
expect(await storage.getAll()).toMatchObject(expectedObj);
expect(await storage.get(`${key}.2`)).toBe(3);
});

test('Getting a nested values from an array', async () => {
const key: string = 'my_array.my_nested_array';
const value: any[] = [1, 2, 3, 4, { other_array: 'Hello from here!' }];
const expectedObj: any = {
my_array: {
my_nested_array: value,
},
};

await storage.set(key, value);

expect(await storage.getAll()).toMatchObject(expectedObj);
expect(await storage.get(`${key}.2`)).toBe(3);
expect(await storage.get(`${key}.4.other_array`)).toBe('Hello from here!');
});

afterEach(async () => {
await storage.clean();
});

afterAll(async () => {
await fsPromises.unlink(storage.path);
});
Expand Down Expand Up @@ -248,6 +315,10 @@ describe('Testing deleting existing values', () => {
expect(await storage.getAll()).toMatchObject(expectedObj);
});

afterEach(async () => {
await storage.clean();
});

afterAll(async () => {
await fsPromises.unlink(storage.path);
});
Expand Down Expand Up @@ -301,6 +372,10 @@ describe('Testing existance of values', () => {
expect(await storage.exists(key)).toBe(false);
});

afterEach(async () => {
await storage.clean();
});

afterAll(async () => {
await fsPromises.unlink(storage.path);
});
Expand Down

0 comments on commit 46356ba

Please sign in to comment.