Skip to content

Commit

Permalink
cr comments - adding tests and some refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitzperetz committed Dec 2, 2024
1 parent 4a291bc commit 2023969
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const createUserSdk = ({

const json = await res.json();

return json.data;
return json.data || [];
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type Sdk = ReturnType<typeof createWebSdk>;

type CustomAttributeType = string | boolean | number;

export type CustomAttributes = Record<string, CustomAttributeType>;
type CustomAttributes = Record<string, CustomAttributeType>;

type UserStatus = 'enabled' | 'disabled' | 'invited';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export const initUserCustomAttributesMixin = createSingletonMixin(
initWidgetRootMixin,
modalMixin,
)(superclass) {
customValueUserAttr: UserAttributeDriver;

// flow Id is key in all maps
#editModals: Record<string, ModalDriver> = {};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSelector } from 'reselect';
import { AttributeType } from '../api/types';
import { AttributeType, CustomAttr } from '../api/types';
import { State } from './types';

export const getMe = (state: State) => state.me.data;
Expand All @@ -25,26 +25,32 @@ export const getCustomAttributes = (state: State) =>
export const getUserCustomAttrs = createSelector(
getMe,
getCustomAttributes,
(userData, allCustomAttrs = []) => {
const res: Record<string, string> = {};
(
userData: Record<string, any>,
allCustomAttrs: CustomAttr[] = [],
): Record<string, string> => {
const userCustomAttributes = userData['customAttributes'] || {};

Object.keys(userCustomAttributes).forEach((key: string) => {
const type =
allCustomAttrs.find((attr) => attr.name === key)?.type ||
AttributeType.text;
if (type === AttributeType.date && userCustomAttributes[key]) {
// to full date time
res[key] = new Date(userCustomAttributes[key]).toLocaleString();
} else if (
type === AttributeType.boolean &&
userCustomAttributes[key] !== undefined
) {
res[key] = !userCustomAttributes[key] ? 'False' : 'True';
} else {
res[key] = (userCustomAttributes[key] || '').toString();
}
});
return res;
return Object.entries(userCustomAttributes).reduce(
(acc, [key]) => {
const type =
allCustomAttrs.find((attr) => attr.name === key)?.type ||
AttributeType.text;

if (type === AttributeType.date && userCustomAttributes[key]) {
// to full date time
acc[key] = new Date(userCustomAttributes[key]).toLocaleString();
} else if (
type === AttributeType.boolean &&
userCustomAttributes[key] !== undefined
) {
acc[key] = !userCustomAttributes[key] ? 'False' : 'True';
} else {
acc[key] = (userCustomAttributes[key] || '').toString();
}
return acc;
},
{} as Record<string, string>,
);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,22 @@ describe('user-profile-widget', () => {

expect(result).toEqual(mockUser);
});

it('custom attributes', async () => {
mockHttpClient.reset();
const sdk = createSdk({ projectId: mockProjectId }, false);
const result = await sdk.user.getCustomAttributes();

await waitFor(() => expect(mockHttpClient.get).toHaveBeenCalledTimes(1), {
timeout: 5000,
});
await waitFor(() =>
expect(mockHttpClient.get).toHaveBeenCalledWith(
apiPaths.user.customAttributes,
),
);

expect(result).toEqual([]);
});
});
});

0 comments on commit 2023969

Please sign in to comment.