Skip to content

Commit

Permalink
fixed typecheck issues, and improved unit-test descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
framitdavid committed Oct 29, 2024
1 parent 179ba31 commit c0ed021
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ describe('useOrgAppScopedStorage', () => {
window.sessionStorage.clear();
});

it.each(storagesToTest)('initializes ScopedStorageImpl with correct storage scope', (storage) => {
const result = renderUseOrgAppScopedStorage({ storage });

result.current.setItem('key', 'value');

expect(result.current.setItem).toBeDefined();
expect(result.current.getItem).toBeDefined();
expect(result.current.removeItem).toBeDefined();
expect(window[storage].getItem(scopedStorageKey)).toBe('{"key":"value"}');
});
it.each(storagesToTest)(
'initializes ScopedStorageImpl with correct storage scope, %s',
(storage) => {
const result = renderUseOrgAppScopedStorage({ storage });

Check failure on line 23 in frontend/libs/studio-hooks/src/hooks/useOrgAppScopedStorage.test.tsx

View workflow job for this annotation

GitHub Actions / Typechecking and linting

`result` is not a recommended name for `render` returned value. Instead, you should destructure it, or name it using one of: `view`, or `utils`

result.current.setItem('key', 'value');

expect(result.current.setItem).toBeDefined();
expect(result.current.getItem).toBeDefined();
expect(result.current.removeItem).toBeDefined();
expect(window[storage].getItem(scopedStorageKey)).toBe('{"key":"value"}');
},
);

it.each(storagesToTest)('should retrieve parsed objects from storage', (storage) => {
it.each(storagesToTest)('should retrieve parsed objects from %s', (storage) => {
const result = renderUseOrgAppScopedStorage({ storage });

Check failure on line 35 in frontend/libs/studio-hooks/src/hooks/useOrgAppScopedStorage.test.tsx

View workflow job for this annotation

GitHub Actions / Typechecking and linting

`result` is not a recommended name for `render` returned value. Instead, you should destructure it, or name it using one of: `view`, or `utils`

result.current.setItem('person', { name: 'John', age: 18 });
Expand All @@ -39,7 +42,7 @@ describe('useOrgAppScopedStorage', () => {
});
});

it.each(storagesToTest)('should be possible to remove item from storage', (storage) => {
it.each(storagesToTest)('should be possible to remove item from %s', (storage) => {
const result = renderUseOrgAppScopedStorage({ storage });

Check failure on line 46 in frontend/libs/studio-hooks/src/hooks/useOrgAppScopedStorage.test.tsx

View workflow job for this annotation

GitHub Actions / Typechecking and linting

`result` is not a recommended name for `render` returned value. Instead, you should destructure it, or name it using one of: `view`, or `utils`

result.current.setItem('key', 'value');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { useParams } from 'react-router-dom';
import { type ScopedStorage, ScopedStorageImpl } from '@studio/pure-functions';
import {
type ScopedStorage,
type ScopedStorageResult,
ScopedStorageImpl,
} from '@studio/pure-functions';

type OrgAppParams = {
org: string;
Expand All @@ -15,8 +19,7 @@ export type UseOrgAppScopedStorage = {
storage?: 'localStorage' | 'sessionStorage';
};

type UseOrgAppScopedStorageResult = ScopedStorage;

type UseOrgAppScopedStorageResult = ScopedStorageResult;
export const useOrgAppScopedStorage = ({
storage = 'localStorage',
}: UseOrgAppScopedStorage): UseOrgAppScopedStorageResult => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
type StorageKey = string;

export interface ScopedStorage extends Pick<Storage, 'setItem' | 'getItem' | 'removeItem'> {
setItem: <T>(key: string, value: T) => T;
getItem: <T>(key: string) => T | null;
export interface ScopedStorage extends Pick<Storage, 'setItem' | 'getItem' | 'removeItem'> {}

export interface ScopedStorageResult extends ScopedStorage {
setItem: <T>(key: string, value: T) => void;
getItem: <T>(key: string) => T;
removeItem: (key: string) => void;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { ScopedStorageImpl, type ScopedStorage } from './ScopedStorage';
export { ScopedStorageImpl, type ScopedStorage, type ScopedStorageResult } from './ScopedStorage';

0 comments on commit c0ed021

Please sign in to comment.