Skip to content

Commit

Permalink
PR Feedback: make flashMessages method names more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
cee-chen committed Aug 26, 2020
1 parent 28275c1 commit ec50b60
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('FlashMessagesLogic', () => {
expect(FlashMessagesLogic.values).toEqual(DEFAULT_VALUES);
});

describe('setMessages()', () => {
describe('setFlashMessages()', () => {
it('sets an array of messages', () => {
const messages = [
{ type: 'success', message: 'Hello world!!' },
Expand All @@ -34,7 +34,7 @@ describe('FlashMessagesLogic', () => {
] as IFlashMessage[];

FlashMessagesLogic.mount();
FlashMessagesLogic.actions.setMessages(messages);
FlashMessagesLogic.actions.setFlashMessages(messages);

expect(FlashMessagesLogic.values.messages).toEqual(messages);
});
Expand All @@ -43,17 +43,17 @@ describe('FlashMessagesLogic', () => {
const message = { type: 'success', message: 'I turn into an array!' } as IFlashMessage;

FlashMessagesLogic.mount();
FlashMessagesLogic.actions.setMessages(message);
FlashMessagesLogic.actions.setFlashMessages(message);

expect(FlashMessagesLogic.values.messages).toEqual([message]);
});
});

describe('clearMessages()', () => {
describe('clearFlashMessages()', () => {
it('sets messages back to an empty array', () => {
FlashMessagesLogic.mount();
FlashMessagesLogic.actions.setMessages('test' as any);
FlashMessagesLogic.actions.clearMessages();
FlashMessagesLogic.actions.setFlashMessages('test' as any);
FlashMessagesLogic.actions.clearFlashMessages();

expect(FlashMessagesLogic.values.messages).toEqual([]);
});
Expand Down Expand Up @@ -94,8 +94,8 @@ describe('FlashMessagesLogic', () => {
it('listens for history changes and clears messages on change', () => {
FlashMessagesLogic.mount();
FlashMessagesLogic.actions.setQueuedMessages(['queuedMessages'] as any);
jest.spyOn(FlashMessagesLogic.actions, 'clearMessages');
jest.spyOn(FlashMessagesLogic.actions, 'setMessages');
jest.spyOn(FlashMessagesLogic.actions, 'clearFlashMessages');
jest.spyOn(FlashMessagesLogic.actions, 'setFlashMessages');
jest.spyOn(FlashMessagesLogic.actions, 'clearQueuedMessages');
jest.spyOn(FlashMessagesLogic.actions, 'setHistoryListener');

Expand All @@ -108,8 +108,10 @@ describe('FlashMessagesLogic', () => {

const mockHistoryChange = (mockListener.mock.calls[0] as any)[0];
mockHistoryChange();
expect(FlashMessagesLogic.actions.clearMessages).toHaveBeenCalled();
expect(FlashMessagesLogic.actions.setMessages).toHaveBeenCalledWith(['queuedMessages']);
expect(FlashMessagesLogic.actions.clearFlashMessages).toHaveBeenCalled();
expect(FlashMessagesLogic.actions.setFlashMessages).toHaveBeenCalledWith([
'queuedMessages',
]);
expect(FlashMessagesLogic.actions.clearQueuedMessages).toHaveBeenCalled();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export interface IFlashMessagesLogicValues {
historyListener: Function | null;
}
export interface IFlashMessagesLogicActions {
setMessages(messages: IFlashMessage | IFlashMessage[]): void;
clearMessages(): void;
setFlashMessages(messages: IFlashMessage | IFlashMessage[]): void;
clearFlashMessages(): void;
setQueuedMessages(messages: IFlashMessage | IFlashMessage[]): void;
clearQueuedMessages(): void;
listenToHistory(history: History): void;
Expand All @@ -35,8 +35,8 @@ const convertToArray = (messages: IFlashMessage | IFlashMessage[]) =>

export const FlashMessagesLogic = kea({
actions: (): IFlashMessagesLogicActions => ({
setMessages: (messages) => ({ messages: convertToArray(messages) }),
clearMessages: () => null,
setFlashMessages: (messages) => ({ messages: convertToArray(messages) }),
clearFlashMessages: () => null,
setQueuedMessages: (messages) => ({ messages: convertToArray(messages) }),
clearQueuedMessages: () => null,
listenToHistory: (history) => history,
Expand All @@ -46,8 +46,8 @@ export const FlashMessagesLogic = kea({
messages: [
[],
{
setMessages: (_, { messages }) => messages,
clearMessages: () => [],
setFlashMessages: (_, { messages }) => messages,
clearFlashMessages: () => [],
},
],
queuedMessages: [
Expand All @@ -68,8 +68,8 @@ export const FlashMessagesLogic = kea({
listenToHistory: (history) => {
// On React Router navigation, clear previous flash messages and load any queued messages
const unlisten = history.listen(() => {
actions.clearMessages();
actions.setMessages(values.queuedMessages);
actions.clearFlashMessages();
actions.setFlashMessages(values.queuedMessages);
actions.clearQueuedMessages();
});
actions.setHistoryListener(unlisten);
Expand Down

0 comments on commit ec50b60

Please sign in to comment.