This is my first time with Ngrx. I found some tutorials but i still don't know how to make tests to mine implementation of Ngrx. I want to test one action from this code:
import { Action } from '@ngrx/store';
import { SystemSettings} from './system';
export enum SystemSettingsActionsTypes {
UpdateSystemSettings = '[SystemSetting] Update Setting',
UpdateSystemSettingsSuccess = '[SystemSetting] Update Setting Success',
UpdateSystemSettingsFail = '[SystemSetting] Update Setting Fail',
LoadSystemSettings = '[SystemSetting] Load Setting',
LoadSystemSettingsSuccess = '[SystemSetting] Load Setting Success',
LoadSystemSettingsFail = '[SystemSetting] Load Setting Fail'
}
export class UpdateSystemSettings implements Action {
public readonly type = SystemSettingsActionsTypes.UpdateSystemSettings;
constructor(public payload: SystemSettings) { }
}
export class UpdateSystemSettingsSuccess implements Action {
public readonly type = SystemSettingsActionsTypes.UpdateSystemSettingsSuccess;
constructor(public payload: SystemSettings) { }
}
export class UpdateSystemSettingsFail implements Action {
public readonly type = SystemSettingsActionsTypes.UpdateSystemSettingsFail;
constructor(public payload: string) { }
}
export class LoadSystemSettings implements Action {
public readonly type = SystemSettingsActionsTypes.LoadSystemSettings;
constructor() { }
}
export class LoadSystemSettingsSuccess implements Action {
public readonly type = SystemSettingsActionsTypes.LoadSystemSettingsSuccess;
constructor(public payload: SystemSettings) { }
}
export class LoadSystemSettingsFail implements Action {
public readonly type = SystemSettingsActionsTypes.LoadSystemSettingsFail;
constructor(public payload: string) { }
}
export type SystemSettingsActions =
| UpdateSystemSettings
| UpdateSystemSettingsSuccess
| UpdateSystemSettingsFail
| LoadSystemSettings
| LoadSystemSettingsSuccess
| LoadSystemSettingsFail;
At this moment i have that sample of code, but im bad in testing and still don't know how to make it good (good working example on my code will be very helpful).
describe('LoadSystemSettingsSuccessfull', () => {
it('should load system settings successfully', () => {
const payload: SystemSettings[] = [
{
measure: 1,
clock: 1,
dateFormat: 1,
lowQuality: 1,
highQuality: 1
}
];
const action = new LoadSystemSettingsSuccess(payload);
expect({ ...action}).toEqual({
type: LoadSystemSettingsSuccess,
payload,
});
});
});
Your action test looks fine! It often helps to ask yourself Why am I testing this?
, and in this case, you're testing whether the implementation of an action creates the object you're the expecting.
While these test scenarios are often used as an introduction to testing NGRX, they are probably also the most useless ones and - in my eyes - often do not justify the effort.
Instead, focus on testing the following elements of NGRX:
These are the main building blocks responsible for you app functionality und should be thoroughly tested.