Skip to content
Open
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
2 changes: 1 addition & 1 deletion .sizelimit.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"limits": {
"total": 550000,
"totalGzip": 200000,
"totalGzip": 250000,
"css": 150000,
"cssGzip": 25000,
"maxChunkSize": 50000,
Expand Down
3 changes: 3 additions & 0 deletions example/src/Onboarding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ const OnboardingWithProps = ({
externalId={externalId}
options={{
features: ['onboarding_reserves'],
jsonSchemaVersion: {
employment_basic_information: 'latest',
},
jsonSchemaVersionByCountry: {
DEU: {
contract_details: 1,
Expand Down
43 changes: 20 additions & 23 deletions src/flows/Onboarding/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import {
JSONSchemaFormType,
FlowOptions,
} from '@/src/flows/types';
import { getContractDetailsSchemaVersion } from '@/src/flows/Onboarding/utils';
import {
getContractDetailsSchemaVersion,
getBasicInformationSchemaVersion,
getBenefitOffersSchemaVersion,
} from '@/src/flows/Onboarding/utils';
import { createHeadlessForm } from '@/src/common/createHeadlessForm';
import { countriesOptions } from '@/src/common/api/countries';

Expand Down Expand Up @@ -232,14 +236,11 @@ export const useBenefitOffersSchema = (
fieldValues: FieldValues,
options: OnboardingFlowProps['options'],
) => {
const jsonSchemaQueryParam = options?.jsonSchemaVersion
?.benefit_offers_form_schema
? {
json_schema_version:
options.jsonSchemaVersion.benefit_offers_form_schema,
}
: {};
const { client } = useClient();
const jsonSchemaQueryParam = {
json_schema_version: getBenefitOffersSchemaVersion(options),
};

return useQuery({
queryKey: ['benefit-offers-schema', employmentId],
retry: false,
Expand Down Expand Up @@ -274,11 +275,12 @@ export const useBenefitOffersSchema = (
* Use this hook to create an employment
* @returns
*/
export const useCreateEmployment = () => {
export const useCreateEmployment = (
options?: OnboardingFlowProps['options'],
) => {
const { client } = useClient();
// TODO: setting 1 as basic_information only supports v1 for now in the API
const jsonSchemaQueryParam = {
json_schema_version: 1,
json_schema_version: getBasicInformationSchemaVersion(options),
};
return useMutation({
mutationFn: (payload: EmploymentCreateParams) => {
Expand All @@ -302,8 +304,8 @@ export const useUpdateEmployment = (
) => {
const { client } = useClient();
const jsonSchemaQueryParams = {
// TODO: setting 1 as basic_information only supports v1 for now in the API
employment_basic_information_json_schema_version: 1,
employment_basic_information_json_schema_version:
getBasicInformationSchemaVersion(options),
contract_details_json_schema_version:
getContractDetailsSchemaVersion(options, countryCode) || 1,
};
Expand Down Expand Up @@ -342,22 +344,17 @@ export const useUpdateBenefitsOffers = (
}: UnifiedEmploymentUpsertBenefitOffersRequest & {
employmentId: string;
}) => {
const jsonSchemaQueryParam = options?.jsonSchemaVersion
?.benefit_offers_form_schema
? {
json_schema_version:
options.jsonSchemaVersion.benefit_offers_form_schema,
}
: {};
const jsonSchemaQueryParam = {
json_schema_version: getBenefitOffersSchemaVersion(options),
};

return putUpdateBenefitOffer({
client: client as Client,
body: payload,
path: {
employment_id: employmentId,
},
query: {
...jsonSchemaQueryParam,
},
query: jsonSchemaQueryParam,
});
},
});
Expand Down
5 changes: 3 additions & 2 deletions src/flows/Onboarding/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useStepState, Step } from '@/src/flows/useStepState';
import {
disabledInviteButtonEmploymentStatus,
getContractDetailsSchemaVersion,
getBasicInformationSchemaVersion,
reviewStepAllowedEmploymentStatus,
STEPS,
STEPS_WITHOUT_SELECT_COUNTRY,
Expand Down Expand Up @@ -233,7 +234,7 @@ export const useOnboarding = ({
},
});

const createEmploymentMutation = useCreateEmployment();
const createEmploymentMutation = useCreateEmployment(options);
const updateEmploymentMutation = useUpdateEmployment(
internalCountryCode as string,
options,
Expand Down Expand Up @@ -328,7 +329,7 @@ export const useOnboarding = ({
enabled: isBasicInformationDetailsEnabled,
},
},
jsonSchemaVersion: 1,
jsonSchemaVersion: getBasicInformationSchemaVersion(options),
});

const annualGrossSalaryField =
Expand Down
186 changes: 186 additions & 0 deletions src/flows/Onboarding/tests/useOnboardingJsonSchemaVersion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const mockGetShowSchema = vi.fn();
const mockPostCreateEmployment2 = vi.fn();
const mockPatchUpdateEmployment2 = vi.fn();
const mockPutUpdateBenefitOffer = vi.fn();
const mockPostCreateContractEligibility = vi.fn();

// Mock the client functions
vi.mock('@/src/client', () => ({
Expand All @@ -23,6 +24,8 @@ vi.mock('@/src/client', () => ({
mockPatchUpdateEmployment2(...args),
putUpdateBenefitOffer: (...args: $TSFixMe[]) =>
mockPutUpdateBenefitOffer(...args),
postCreateContractEligibility: (...args: $TSFixMe[]) =>
mockPostCreateContractEligibility(...args),
}));

const queryClient = new QueryClient({
Expand Down Expand Up @@ -73,6 +76,10 @@ describe('useOnboarding jsonSchemaVersion', () => {
data: { data: { success: true } },
});

mockPostCreateContractEligibility.mockResolvedValue({
data: { data: { status: 'ok' } },
});

// Mock server responses
server.use(
http.get('*/v1/companies/:companyId', () => {
Expand Down Expand Up @@ -199,6 +206,73 @@ describe('useOnboarding jsonSchemaVersion', () => {
});
});
});

it('should pass custom jsonSchemaVersion to basic information form', async () => {
const options = {
jsonSchemaVersion: {
employment_basic_information: 2,
},
};
const { result } = renderHook(
() =>
useOnboarding({
companyId: 'test-company-id',
countryCode: 'PRT',
employmentId: 'test-employment-id',
skipSteps: ['select_country'],
options,
}),
{ wrapper: TestProviders },
);
await waitFor(() => {
expect(result.current.isLoading).toBe(false);
});
await waitFor(() => {
expect(mockGetShowFormCountry).toHaveBeenCalled();
});
const call = mockGetShowFormCountry.mock.calls[0][0];
expect(call.path).toEqual({
country_code: 'PRT',
form: 'employment_basic_information',
});
expect(call.query).toEqual({
skip_benefits: true,
json_schema_version: 2,
});
});

it('should pass custom benefit_offers_form_schema version to getShowSchema', async () => {
const options = {
jsonSchemaVersion: {
benefit_offers_form_schema: 2,
},
};
const { result } = renderHook(
() =>
useOnboarding({
companyId: 'test-company-id',
countryCode: 'PRT',
employmentId: 'test-employment-id',
skipSteps: ['select_country'],
options,
}),
{ wrapper: TestProviders },
);
await waitFor(() => {
expect(result.current.isLoading).toBe(false);
});
// Navigate to benefits step
act(() => {
result.current.goTo('benefits');
});
await waitFor(() => {
expect(mockGetShowSchema).toHaveBeenCalled();
});
const call = mockGetShowSchema.mock.calls[0][0];
expect(call.query).toEqual({
json_schema_version: 2,
});
});
});

describe('useUpdateEmployment calls', () => {
Expand Down Expand Up @@ -294,5 +368,117 @@ describe('useOnboarding jsonSchemaVersion', () => {
contract_details_json_schema_version: 2,
});
});

it('should call patchUpdateEmployment2 with custom employment_basic_information_json_schema_version when submitting basic_information', async () => {
const { result } = renderHook(
() =>
useOnboarding({
employmentId: 'existing-employment-id',
companyId: 'test-company-id',
countryCode: 'ARG',
skipSteps: ['select_country'],
options: {
jsonSchemaVersion: {
employment_basic_information: 3, // ← Test this!
},
},
}),
{ wrapper: TestProviders },
);
await waitFor(() => {
expect(result.current.isLoading).toBe(false);
});
// Submit basic_information form (first step)
await act(async () => {
await result.current.onSubmit({
name: 'John Doe',
email: 'john@example.com',
});
});
await waitFor(() => {
expect(mockPatchUpdateEmployment2).toHaveBeenCalled();
});
const call = mockPatchUpdateEmployment2.mock.calls[0][0];
expect(call.query).toEqual({
skip_benefits: true,
employment_basic_information_json_schema_version: 3, // ← Assert this
contract_details_json_schema_version: 1, // default
});
});
});

describe('useCreateEmployment calls', () => {
it('should call postCreateEmployment2 with default json_schema_version when creating new employment', async () => {
const { result } = renderHook(
() =>
useOnboarding({
// No employmentId = create new
companyId: 'test-company-id',
countryCode: 'PRT',
skipSteps: ['select_country'],
}),
{ wrapper: TestProviders },
);

await waitFor(() => {
expect(result.current.isLoading).toBe(false);
});

// Submit basic_information to create employment
await act(async () => {
await result.current.onSubmit({
name: 'John Doe',
email: 'john@example.com',
});
});

await waitFor(() => {
expect(mockPostCreateEmployment2).toHaveBeenCalled();
});

const call = mockPostCreateEmployment2.mock.calls[0][0];

expect(call.query).toEqual({
json_schema_version: 1,
});
});

it('should call postCreateEmployment2 with custom employment_basic_information_json_schema_version', async () => {
const { result } = renderHook(
() =>
useOnboarding({
companyId: 'test-company-id',
countryCode: 'ARG',
skipSteps: ['select_country'],
options: {
jsonSchemaVersion: {
employment_basic_information: 3,
},
},
}),
{ wrapper: TestProviders },
);

await waitFor(() => {
expect(result.current.isLoading).toBe(false);
});

await act(async () => {
await result.current.onSubmit({
name: 'John Doe',
email: 'john@example.com',
});
});

await waitFor(() => {
expect(mockPostCreateEmployment2).toHaveBeenCalled();
});

const call = mockPostCreateEmployment2.mock.calls[0][0];

expect(call.query).toEqual({
json_schema_version: 3,
});
});
});
});
Loading
Loading