Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: GEO 1088 display error snackbar when download file fails #774

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,28 @@ import { Announcement, AnnouncementResource } from '../../types/announcements';
import { sanitizeUrl } from '@braintree/sanitize-url';
import ApiService from '../../services/apiService';
import { saveAs } from 'file-saver';
import { NotificationService } from '../../services/notificationService';

const props = defineProps<{
defineProps<{
announcement: Announcement;
}>();

async function downloadAnnouncementResource(
announcementResource: AnnouncementResource,
) {
if (announcementResource.announcement_resource_id) {
await ApiService.downloadFile(
announcementResource.announcement_resource_id,
);
try {
await ApiService.downloadFile(
announcementResource.announcement_resource_id,
);
} catch (error) {
console.error(error);
NotificationService.pushNotificationError(
'There is a problem with this link/file, please try again later or contact the helpdesk.',
'',
30000,
);
}
} else if (announcementResource.announcement_resource_file) {
//When a resource with type ATTACHMENT hasn't yet been uploaded to the
//backend, it won't yet have an announcement_resource_id, so we
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ vi.mock('../../../services/apiService', () => ({
downloadFile: (...args) => mockDownloadFile(...args),
},
}));

const pushNotificationErrorMock = vi.fn();
vi.mock('../../../services/notificationService', () => ({
NotificationService: {
pushNotificationError: (...args) => pushNotificationErrorMock(...args),
},
}));

vi.mock('file-saver', () => ({
saveAs: (...args) => mockSaveAs(...args),
}));
Expand Down Expand Up @@ -72,6 +80,7 @@ describe('AnnouncementItem', () => {

beforeEach(async () => {
await initWrapper();
vi.clearAllMocks();
});

afterEach(() => {
Expand Down Expand Up @@ -102,5 +111,20 @@ describe('AnnouncementItem', () => {
expect(mockSaveAs).toHaveBeenCalled();
});
});

describe('when downloadFile fails', () => {
it('shows a snackbar error', async () => {
const mockAnnouncementResource = {
announcement_resource_id: '123',
};
mockDownloadFile.mockRejectedValueOnce(new Error('mock error'));
await wrapper.vm.downloadAnnouncementResource(mockAnnouncementResource);
expect(pushNotificationErrorMock).toHaveBeenCalledWith(
'There is a problem with this link/file, please try again later or contact the helpdesk.',
'',
30000,
);
});
});
});
});
16 changes: 14 additions & 2 deletions frontend/src/components/announcements/AnnouncementItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,26 @@ export default {
import { Announcement, AnnouncementResource } from '../../types/announcements';
import { sanitizeUrl } from '@braintree/sanitize-url';
import ApiService from '../../common/apiService';
import { NotificationService } from '../../common/notificationService';

const props = defineProps<{
defineProps<{
announcement: Announcement;
}>();

async function downloadAnnouncementResource(
announcementResource: AnnouncementResource,
) {
await ApiService.downloadFile(announcementResource.announcement_resource_id);
try {
await ApiService.downloadFile(
announcementResource.announcement_resource_id,
);
} catch (error) {
console.error(error);
NotificationService.pushNotificationError(
'There is a problem with this link/file, please try again later. If the problem persists please contact the Gender Equity Office; paytransparency@gov.bc.ca',
'',
30000,
);
}
}
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ vi.mock('../../../common/apiService', () => ({
},
}));

const pushNotificationErrorMock = vi.fn();
vi.mock('../../../common/notificationService', () => ({
NotificationService: {
pushNotificationError: (...args) => pushNotificationErrorMock(...args),
},
}));

// Stub blobal objects needed for testing
vi.stubGlobal('ResizeObserver', ResizeObserverMock);
vi.stubGlobal('URL', { createObjectURL: vi.fn() });
Expand Down Expand Up @@ -89,5 +96,20 @@ describe('AnnouncementItem', () => {
expect(mockSaveAs).not.toHaveBeenCalled();
});
});

describe('when downloadFile fails', () => {
it('shows a snackbar error', async () => {
const mockAnnouncementResource = {
announcement_resource_id: '123',
};
mockDownloadFile.mockRejectedValueOnce(new Error('mock error'));
await wrapper.vm.downloadAnnouncementResource(mockAnnouncementResource);
expect(pushNotificationErrorMock).toHaveBeenCalledWith(
'There is a problem with this link/file, please try again later. If the problem persists please contact the Gender Equity Office; paytransparency@gov.bc.ca',
'',
30000,
);
});
});
});
});