Files
api/web/__test__/mocks/apollo-client.ts
Michael Datelle 03be042410 test: create tests for stores (#1338)
This gets the original 3 component tests refactored to better follow the
Vue Testing Library philosophy and test behavior. This also adds a new
test file for the server store. Additional batches of tests will be
added in proceeding PR's.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Chores**  
- Streamlined internal code organization and improved maintenance
through refined import structures and cleanup of redundant files.

- **Tests**  
- Expanded and restructured automated tests across core components,
including new test files for `Auth`, `DownloadApiLogs`, and `KeyActions`
to ensure robust behavior.
- Enhanced test configuration and mock implementations for a more
reliable, consistent testing environment.
- Introduced best practices for testing Vue components and Pinia stores.

These updates optimize performance and stability behind the scenes
without altering the end-user experience.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: mdatelle <mike@datelle.net>
2025-04-09 11:57:11 -04:00

48 lines
1.2 KiB
TypeScript

import { provideApolloClient } from '@vue/apollo-composable';
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client/core';
// Types for Apollo Client options
interface TestApolloClientOptions {
uri?: string;
mockData?: Record<string, unknown>;
}
// Single function to create Apollo clients
function createClient(options: TestApolloClientOptions = {}) {
const { uri = 'http://localhost/graphql', mockData = { data: {} } } = options;
return new ApolloClient({
link: createHttpLink({
uri,
credentials: 'include',
fetch: () => Promise.resolve(new Response(JSON.stringify(mockData))),
}),
cache: new InMemoryCache(),
defaultOptions: {
watchQuery: {
fetchPolicy: 'no-cache',
},
},
});
}
// Default mock client
export const mockApolloClient = createClient();
// Helper function to provide the mock client
export function provideMockApolloClient() {
provideApolloClient(mockApolloClient);
return mockApolloClient;
}
// Create a customizable Apollo Client
export function createTestApolloClient(options: TestApolloClientOptions = {}) {
const client = createClient(options);
provideApolloClient(client);
return client;
}