Skip to content

Commit

Permalink
feat(components): add ErrorBoundary (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuagraber committed May 28, 2024
1 parent c7fa873 commit 10d351c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/components/ErrorBoundary/PdapErrorBoundary.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<template>
<component
:is="component"
v-if="error"
class="pdap-flex-container-center h-[full]"
>
<h1>Oops, something went wrong!</h1>
<p class="max-w-full" data-test="error-boundary-message">
If you keep seeing this message, please email
<a href="mailto:[email protected]">[email protected]</a> for assistance.
</p>
</component>
<slot v-else />
</template>
<script>
export default {
props: {
component: {
type: String,
default: 'div',
},
},
data() {
return {
error: false,
};
},
/* TODO: figure out how to cover this lifecycle method in tests */
errorCaptured(error) {
this.interceptError(error);
},
methods: {
interceptError(error) {
this.error = error;
},
},
};
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`ErrorBoundary > renders error content with error 1`] = `
<div class="pdap-flex-container-center h-[full]">
<h1>Oops, something went wrong!</h1>
<p class="max-w-full"> If you keep seeing this message, please email <a href="mailto:[email protected]">[email protected]</a> for assistance. </p>
</div>
`;

exports[`ErrorBoundary > renders slot content with no error 1`] = `<div></div>`;
31 changes: 31 additions & 0 deletions src/components/ErrorBoundary/error-boundary.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { mount } from '@vue/test-utils';
import { beforeEach, describe, expect, it } from 'vitest';
import ErrorBoundary from './PdapErrorBoundary.vue';
import { nextTick } from 'vue';

let wrapper;

describe('ErrorBoundary', () => {
beforeEach(() => {
wrapper = mount(ErrorBoundary, {
slots: {
default: '<div data-test="default-slot" />',
},
});
});

it('renders slot content with no error', () => {
expect(wrapper.find('[data-test="default-slot"]').exists()).toBe(true);
expect(wrapper.html()).toMatchSnapshot();
});

it('renders error content with error', async () => {
wrapper.vm.interceptError(new Error('Generic error'));
await nextTick();

expect(wrapper.find('[data-test="error-boundary-message"]').exists()).toBe(
true
);
expect(wrapper.html()).toMatchSnapshot();
});
});

0 comments on commit 10d351c

Please sign in to comment.