Separate auth0 users depending on tenants
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { useQuery, QueryResult } from '@apollo/client';
|
||||
import { useGetTenantByDomain } from '../useGetTenantByDomain';
|
||||
|
||||
jest.mock('@apollo/client', () => ({
|
||||
useQuery: jest.fn(),
|
||||
}));
|
||||
|
||||
describe('useGetTenantByDomain', () => {
|
||||
beforeEach(() => {
|
||||
const result: Partial<QueryResult<any>> = {
|
||||
data: { tenants: [{ domain: 'pilot.twenty.com' }] },
|
||||
loading: false,
|
||||
error: undefined,
|
||||
};
|
||||
(useQuery as jest.Mock).mockImplementation(() => result as QueryResult);
|
||||
});
|
||||
|
||||
it('returns tenant by domain', () => {
|
||||
const { result } = renderHook(() => useGetTenantByDomain());
|
||||
const domain = result.current.tenant?.domain;
|
||||
expect(domain).toEqual(result.current.tenant?.domain);
|
||||
expect(useQuery).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
31
front/src/hooks/tenant/useGetTenantByDomain.tsx
Normal file
31
front/src/hooks/tenant/useGetTenantByDomain.tsx
Normal file
@ -0,0 +1,31 @@
|
||||
import { ApolloError, useQuery } from '@apollo/client';
|
||||
import { gql } from 'graphql-tag';
|
||||
import { Tenant } from '../../interfaces/tenant.interface';
|
||||
|
||||
const GET_TENANT_BY_DOMAIN = gql`
|
||||
query GetTenantByDomain($domain: String!) {
|
||||
tenants(where: { domain: { _eq: $domain } }, limit: 1) {
|
||||
auth0_client_id
|
||||
domain
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
type TenantResult = {
|
||||
loading: boolean;
|
||||
error?: ApolloError;
|
||||
tenant?: Tenant;
|
||||
};
|
||||
|
||||
export const useGetTenantByDomain = (): TenantResult => {
|
||||
const domain = window.location.hostname;
|
||||
const { loading, error, data } = useQuery(GET_TENANT_BY_DOMAIN, {
|
||||
variables: { domain },
|
||||
context: {
|
||||
headers: {
|
||||
'x-hasura-default-role': 'public',
|
||||
},
|
||||
},
|
||||
});
|
||||
return { loading, error, tenant: data?.tenants[0] };
|
||||
};
|
||||
Reference in New Issue
Block a user