Skip to content

Commit

Permalink
Tests for the new selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Vangaorth committed Jan 24, 2024
1 parent 27d3dfd commit 8e39822
Showing 1 changed file with 226 additions and 1 deletion.
227 changes: 226 additions & 1 deletion ts/features/messages/store/reducers/__tests__/downloads.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,235 @@
import * as pot from "@pagopa/ts-commons/lib/pot";
import { mockPdfAttachment } from "../../../__mocks__/attachment";
import { downloadAttachment, removeCachedAttachment } from "../../actions";
import { Downloads, downloadsReducer } from "../downloads";
import {
Download,
DownloadError,
Downloads,
INITIAL_STATE,
downloadedMessageAttachmentSelector,
downloadsReducer
} from "../downloads";
import {
UIAttachment,
UIAttachmentId,
UIMessageId,
WithSkipMixpanelTrackingOnFailure
} from "../../../types";
import { GlobalState } from "../../../../../store/reducers/types";

const path = "/path/attachment.pdf";

describe("downloadedMessageAttachmentSelector", () => {
it("Should return undefined for an unmatching messageId", () => {
const attachmentId = "1" as UIAttachmentId;
const successDownload = {
attachment: {
messageId: "01HMXFQ803Q8JGQECKQF0EX6KX" as UIMessageId,
id: attachmentId
} as UIAttachment,
path: "randomPath"
} as Download;
const downloadSuccessAction = downloadAttachment.success(successDownload);
const downloadsState = downloadsReducer(
INITIAL_STATE,
downloadSuccessAction
);
const globalState = {
entities: {
messages: {
downloads: downloadsState
}
}
} as GlobalState;
const messageId = "01HMXFE7192J01KNK02BJAPMBR" as UIMessageId;
const downloadedAttachment = downloadedMessageAttachmentSelector(
globalState,
messageId,
attachmentId
);
expect(downloadedAttachment).toBeUndefined();
});
it("Should return undefined for a matching messageId with an unmatching attachmentId", () => {
const messageId = "01HMXFE7192J01KNK02BJAPMBR" as UIMessageId;
const unrelatedAttachmentId = "2";
const successDownload = {
attachment: {
messageId,
id: unrelatedAttachmentId
} as UIAttachment,
path: "randomPath"
} as Download;
const downloadSuccessAction = downloadAttachment.success(successDownload);
const downloadsState = downloadsReducer(
INITIAL_STATE,
downloadSuccessAction
);
const globalState = {
entities: {
messages: {
downloads: downloadsState
}
}
} as GlobalState;
const attachmentId = "1" as UIAttachmentId;
const downloadedAttachment = downloadedMessageAttachmentSelector(
globalState,
messageId,
attachmentId
);
expect(downloadedAttachment).toBeUndefined();
});
it("Should return undefined for an attachment that is loading", () => {
const messageId = "01HMXFE7192J01KNK02BJAPMBR" as UIMessageId;
const attachmentId = "1" as UIAttachmentId;
const uiAttachmentRequest = {
messageId,
id: attachmentId,
skipMixpanelTrackingOnFailure: true
} as WithSkipMixpanelTrackingOnFailure<UIAttachment>;
const downloadRequestAction =
downloadAttachment.request(uiAttachmentRequest);
const downloadsState = downloadsReducer(
INITIAL_STATE,
downloadRequestAction
);
const globalState = {
entities: {
messages: {
downloads: downloadsState
}
}
} as GlobalState;
const downloadedAttachment = downloadedMessageAttachmentSelector(
globalState,
messageId,
attachmentId
);
expect(downloadedAttachment).toBeUndefined();
});
it("Should return undefined for an attachment that got an error", () => {
const messageId = "01HMXFE7192J01KNK02BJAPMBR" as UIMessageId;
const attachmentId = "1" as UIAttachmentId;
const failedDownload = {
attachment: {
messageId,
id: attachmentId
} as UIAttachment,
error: new Error("An error")
} as DownloadError<Error>;
const downloadFailureAction = downloadAttachment.failure(failedDownload);
const downloadsState = downloadsReducer(
INITIAL_STATE,
downloadFailureAction
);
const globalState = {
entities: {
messages: {
downloads: downloadsState
}
}
} as GlobalState;
const downloadedAttachment = downloadedMessageAttachmentSelector(
globalState,
messageId,
attachmentId
);
expect(downloadedAttachment).toBeUndefined();
});
it("Should return undefined for an attachment that was cancelled before finishing the download", () => {
const messageId = "01HMXFE7192J01KNK02BJAPMBR" as UIMessageId;
const attachmentId = "1" as UIAttachmentId;
const uiAttachmentCancelled = {
messageId,
id: attachmentId
} as UIAttachment;
const downloadCancelAction = downloadAttachment.cancel(
uiAttachmentCancelled
);
const downloadsState = downloadsReducer(
INITIAL_STATE,
downloadCancelAction
);
const globalState = {
entities: {
messages: {
downloads: downloadsState
}
}
} as GlobalState;
const downloadedAttachment = downloadedMessageAttachmentSelector(
globalState,
messageId,
attachmentId
);
expect(downloadedAttachment).toBeUndefined();
});
it("Should return undefined for an attachment that was removed by a removeCachedAttachment action", () => {
const messageId = "01HMXFE7192J01KNK02BJAPMBR" as UIMessageId;
const attachmentId = "1" as UIAttachmentId;
const successDownload = {
attachment: {
messageId,
id: attachmentId
} as UIAttachment,
path: "randomPath"
} as Download;
const removedCachedAttachmentAction =
removeCachedAttachment(successDownload);
const downloadsState = downloadsReducer(
INITIAL_STATE,
removedCachedAttachmentAction
);
const globalState = {
entities: {
messages: {
downloads: downloadsState
}
}
} as GlobalState;
const downloadedAttachment = downloadedMessageAttachmentSelector(
globalState,
messageId,
attachmentId
);
expect(downloadedAttachment).toBeUndefined();
});
it("Should return data for a matching downloaded attachment", () => {
const messageId = "01HMXFE7192J01KNK02BJAPMBR" as UIMessageId;
const attachmentId = "1" as UIAttachmentId;
const downloadPath = "randomPath";
const successDownload = {
attachment: {
messageId,
id: attachmentId
} as UIAttachment,
path: downloadPath
} as Download;
const downloadSuccessAction = downloadAttachment.success(successDownload);
const downloadsState = downloadsReducer(
INITIAL_STATE,
downloadSuccessAction
);
const globalState = {
entities: {
messages: {
downloads: downloadsState
}
}
} as GlobalState;
const downloadedAttachment = downloadedMessageAttachmentSelector(
globalState,
messageId,
attachmentId
);
expect(downloadedAttachment).toBeDefined();
expect(downloadedAttachment?.attachment).toBeDefined();
expect(downloadedAttachment?.attachment.messageId).toBe(messageId);
expect(downloadedAttachment?.attachment.id).toBe(attachmentId);
expect(downloadedAttachment?.path).toBe(downloadPath);
});
});

describe("downloadsReducer", () => {
describe("given no download", () => {
const initialState = {};
Expand Down

0 comments on commit 8e39822

Please sign in to comment.