Skip to content

Commit

Permalink
Merge pull request #11 from onemanfighter/amitraikwar-rs/ARMP-99/add-…
Browse files Browse the repository at this point in the history
…setting-store

[Setup]: Adding setting store and wallpaper slice
  • Loading branch information
onemanfighter authored May 25, 2024
2 parents 98bed20 + 6ceff72 commit 3344bac
Show file tree
Hide file tree
Showing 22 changed files with 178 additions and 14 deletions.
6 changes: 6 additions & 0 deletions craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ module.exports = {
__dirname,
'./src/store/processStore/selector',
),
// Settings store
'@settingsStore': path.resolve(__dirname, './src/store/settingsStore'),
'@settingsStore/selector': path.resolve(
__dirname,
'./src/store/settingsStore/selector',
),
},
},
};
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module.exports = {
// Process store
'@processStore': '<rootDir>/src/store/processStore',
'@processStore/selector': '<rootDir>/src/store/processStore/selector',
// Settings store
'@settingsStore': '<rootDir>/src/store/settingsStore',
'@settingsStore/selector': '<rootDir>/src/store/settingsStore/selector',
},
transform: {
'.+\\.(css|scss|png|jpg|svg)$': 'jest-transform-stub',
Expand Down
2 changes: 1 addition & 1 deletion src/store/processStore/processStore/processStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createPowerSlice } from '../slice';
export const processStore = create<ProcessStoreState>()(
persist(
immer((...api) => ({
MainMac: createPowerSlice(...api),
Power: createPowerSlice(...api),
})),
{
name: 'processStore',
Expand Down
2 changes: 1 addition & 1 deletion src/store/processStore/processStore/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StateCreator } from 'zustand';
import { PowerStateSlice } from '../slice';

export interface ProcessStoreState {
MainMac: PowerStateSlice;
Power: PowerStateSlice;
}

export type ProcessStoreSlice<T> = StateCreator<
Expand Down
6 changes: 3 additions & 3 deletions src/store/processStore/selector/Power/Power.selector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ProcessStoreState } from '@processStore';

const powerSelector = (state: ProcessStoreState) => ({
onState: state.MainMac.onState,
turnOn: state.MainMac.turnOn,
turnOff: state.MainMac.turnOff,
onState: state.Power.onState,
turnOn: state.Power.turnOn,
turnOff: state.Power.turnOff,
});

export { powerSelector };
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OnState, processStore } from '@processStore';
import { powerSelector } from '../Power.selector';

describe('Power selector', () => {
it('should return default modal state', () => {
it('should return default power state', () => {
const { result } = renderHook(() => processStore(powerSelector));

expect(result.current.onState).toEqual(OnState.OFF);
Expand Down
4 changes: 2 additions & 2 deletions src/store/processStore/slice/Modal/Power.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const defaultPowerState: PowerState = {

const createPowerSlice: ProcessStoreSlice<PowerStateSlice> = (set) => ({
...defaultPowerState,
turnOn: () => set((state) => void (state.MainMac.onState = OnState.ON)),
turnOff: () => set((state) => void (state.MainMac.onState = OnState.OFF)),
turnOn: () => set((state) => void (state.Power.onState = OnState.ON)),
turnOff: () => set((state) => void (state.Power.onState = OnState.OFF)),
});

export default createPowerSlice;
10 changes: 5 additions & 5 deletions src/store/processStore/slice/Modal/__tests__/Power.slice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ describe('Power slice', () => {
it('should return default on state', () => {
const { result } = renderHook(() => processStore());

expect(result.current.MainMac.onState).toEqual(OnState.OFF);
expect(result.current.Power.onState).toEqual(OnState.OFF);
});

it('should return on stat on calling turnOn', () => {
const { result } = renderHook(() => processStore());

result.current.MainMac.turnOn();
result.current.Power.turnOn();

expect(result.current.MainMac.onState).toEqual(OnState.ON);
expect(result.current.Power.onState).toEqual(OnState.ON);
});

it('should return on stat off calling turnOff', () => {
const { result } = renderHook(() => processStore());

result.current.MainMac.turnOff();
result.current.Power.turnOff();

expect(result.current.MainMac.onState).toEqual(OnState.OFF);
expect(result.current.Power.onState).toEqual(OnState.OFF);
});
});
2 changes: 2 additions & 0 deletions src/store/settingsStore/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './settingsStore';
export * from './slice';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { SettingsStoreState } from '@settingsStore';

const wallpaperSelector = (state: SettingsStoreState) => ({
wallpaper: state.Wallpaper.type,
setWallpaper: state.Wallpaper.setWallpaper,
resetWallpaper: state.Wallpaper.resetWallpaper,
});

export { wallpaperSelector };
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { renderHook } from '@testing-library/react-hooks';
import { wallpaperSelector } from '../Wallpaper.selector';
import { Wallpapers, settingsStore } from '@settingsStore';

describe('Wallpaper selector', () => {
it('should return default wallpaper state', () => {
const { result } = renderHook(() => settingsStore(wallpaperSelector));

expect(result.current.wallpaper).toEqual(Wallpapers.ImageA);
});

it('should set wallpaper using setWallpaper method', () => {
const { result } = renderHook(() => settingsStore(wallpaperSelector));

result.current.setWallpaper(Wallpapers.ImageB);

expect(result.current.wallpaper).toEqual(Wallpapers.ImageB);
});

it('should reset wallpaper using resetWallpaper method', () => {
const { result } = renderHook(() => settingsStore(wallpaperSelector));

result.current.resetWallpaper();

expect(result.current.wallpaper).toEqual(Wallpapers.ImageA);
});
});
1 change: 1 addition & 0 deletions src/store/settingsStore/selector/Wallpaper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { wallpaperSelector } from './Wallpaper.selector';
3 changes: 3 additions & 0 deletions src/store/settingsStore/selector/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { useShallow } from 'zustand/react/shallow';

export * from './Wallpaper';
2 changes: 2 additions & 0 deletions src/store/settingsStore/settingsStore/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type { SettingsStoreState, SettingsStoreSlice } from './types';
export { settingsStore } from './settingsStore';
18 changes: 18 additions & 0 deletions src/store/settingsStore/settingsStore/settingsStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { create } from 'zustand';

import { immer } from 'zustand/middleware/immer';
import { createJSONStorage, persist } from 'zustand/middleware';
import { SettingsStoreState } from './types';
import { createWallpaperSlice } from '../slice';

export const settingsStore = create<SettingsStoreState>()(
persist(
immer((...api) => ({
Wallpaper: createWallpaperSlice(...api),
})),
{
name: 'processStore',
storage: createJSONStorage(() => localStorage),
},
),
);
13 changes: 13 additions & 0 deletions src/store/settingsStore/settingsStore/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { StateCreator } from 'zustand';
import { WallpaperStateSlice } from '../slice/Wallpaper/types';

export interface SettingsStoreState {
Wallpaper: WallpaperStateSlice;
}

export type SettingsStoreSlice<T> = StateCreator<
SettingsStoreState,
[['zustand/immer', never]],
[],
T
>;
22 changes: 22 additions & 0 deletions src/store/settingsStore/slice/Wallpaper/Wallpaper.slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SettingsStoreSlice } from '../../settingsStore/types';
import { WallpaperState, WallpaperStateSlice, Wallpapers } from './types';

const defaultWallpaperState: WallpaperState = {
type: Wallpapers.ImageA,
};

const createWallpaperSlice: SettingsStoreSlice<WallpaperStateSlice> = (
set,
) => ({
...defaultWallpaperState,
setWallpaper: (type) =>
set((state) => {
state.Wallpaper.type = type;
}),
resetWallpaper: () =>
set((state) => {
state.Wallpaper.type = defaultWallpaperState.type;
}),
});

export default createWallpaperSlice;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { renderHook } from '@testing-library/react-hooks';
import { settingsStore } from '@settingsStore';
import { Wallpapers } from '../types';

describe('Wallpaper slice', () => {
it('should return default wallpaper type', () => {
const { result } = renderHook(() => settingsStore());

expect(result.current.Wallpaper.type).toEqual(Wallpapers.ImageA);
});

it('should return image type if set using setWallpaper', () => {
const { result } = renderHook(() => settingsStore());

result.current.Wallpaper.setWallpaper(Wallpapers.ImageB);

expect(result.current.Wallpaper.type).toEqual(Wallpapers.ImageB);
});

it('should return default wallpaper if resetWallpaper is called', () => {
const { result } = renderHook(() => settingsStore());

result.current.Wallpaper.resetWallpaper();

expect(result.current.Wallpaper.type).toEqual(Wallpapers.ImageA);
});
});
3 changes: 3 additions & 0 deletions src/store/settingsStore/slice/Wallpaper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as createWallpaperSlice } from './Wallpaper.slice';
export type { WallpaperStateSlice, WallpaperState } from './types';
export { Wallpapers } from './types';
24 changes: 24 additions & 0 deletions src/store/settingsStore/slice/Wallpaper/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export enum Wallpapers {
ImageA = 'ImageA',
ImageB = 'ImageB',
}

export type WallpaperState = {
type: Wallpapers;
};

export interface WallpaperStateAction {
/**
* Turn on the power
* @returns void
*/
setWallpaper: (type: Wallpapers) => void;

/**
* Turn off the power
* @returns void
*/
resetWallpaper: () => void;
}

export type WallpaperStateSlice = WallpaperState & WallpaperStateAction;
1 change: 1 addition & 0 deletions src/store/settingsStore/slice/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Wallpaper';
5 changes: 4 additions & 1 deletion tsconfig.path.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"@uiStore/selector": ["src/store/uiStore/selector"],
// Process store
"@processStore": ["src/store/processStore"],
"@processStore/selector": ["src/store/processStore/selector"]
"@processStore/selector": ["src/store/processStore/selector"],
// Settings store
"@settingsStore": ["src/store/settingsStore"],
"@settingsStore/selector": ["src/store/settingsStore/selector"]
}
}
}

0 comments on commit 3344bac

Please sign in to comment.