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

chore: miscellaneous-updates #85

Merged
merged 2 commits into from
Jun 7, 2024
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
71 changes: 46 additions & 25 deletions src/components/ErrorBoundary/PdapErrorBoundary.vue
Original file line number Diff line number Diff line change
@@ -1,38 +1,59 @@
<template>
<slot v-if="!error$" />
<component
:is="component"
v-if="error"
v-else
class="pdap-flex-container-center h-[full]"
:v-bind="params"
>
<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 setup lang="ts">
import { ComponentPublicInstance, onErrorCaptured, ref } from 'vue';
import { PdapErrorBoundaryProps } from './types';

const props = withDefaults(defineProps<PdapErrorBoundaryProps>(), {
component: 'div',
onError: undefined,
params: undefined,
stopPropagation: false,
});

interface PdapErrorEmitted {
error: Error;
vm: ComponentPublicInstance | null;
info?: string;
}

const emits = defineEmits<{
(event: 'error', { error, vm, info }: PdapErrorEmitted): void;
}>();
const error$ = ref<Error>();
const info$ = ref<string | undefined>('');

function interceptError(
error: Error,
vm: ComponentPublicInstance | null,
info?: string
) {
error$.value = error;
info$.value = info;

props.onError?.(error, vm);
emits('error', { error: error, vm, info });

if (props.stopPropagation) return false;
}

/* Impossible and unwise to try testing this, so we remove from the coverage report */
/* c8 ignore next 3 */
onErrorCaptured((err, vm, info) => {
interceptError(err, vm, info);
});
</script>
25 changes: 24 additions & 1 deletion src/components/ErrorBoundary/error-boundary.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mount } from '@vue/test-utils';
import { beforeEach, describe, expect, it } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import ErrorBoundary from './PdapErrorBoundary.vue';
import { nextTick } from 'vue';

Expand Down Expand Up @@ -28,4 +28,27 @@ describe('ErrorBoundary', () => {
);
expect(wrapper.html()).toMatchSnapshot();
});

it('calls the onError callback when an error occurs', async () => {
const onErrorSpy = vi.fn();

wrapper = mount(ErrorBoundary, {
props: {
onError: onErrorSpy,
},
slots: {
default: '<div>Default Content</div>',
},
});

const testError = new Error('Test Error');
wrapper.vm.interceptError(testError);
await nextTick();

expect(onErrorSpy).toHaveBeenCalledWith({
error: testError,
vm: undefined,
info: undefined,
});
});
});
1 change: 1 addition & 0 deletions src/components/ErrorBoundary/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as ErrorBoundary } from './PdapErrorBoundary.vue';
11 changes: 11 additions & 0 deletions src/components/ErrorBoundary/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ComponentPublicInstance } from 'vue';

export interface PdapErrorBoundaryProps {
component: string;
onError?: (
error: Error,
target?: ComponentPublicInstance | null | undefined
) => void;
params?: Record<string, string>;
stopPropagation?: boolean;
}
64 changes: 0 additions & 64 deletions src/components/FlexContainer/FlexContainer.vue

This file was deleted.

This file was deleted.

37 changes: 0 additions & 37 deletions src/components/FlexContainer/flex-container.spec.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/FlexContainer/index.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/components/FlexContainer/types.ts

This file was deleted.

66 changes: 0 additions & 66 deletions src/components/GridContainer/GridContainer.vue

This file was deleted.

This file was deleted.

Loading
Loading