Skip to content

Commit

Permalink
Update .prettierrc
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinvdBurg committed Apr 24, 2024
1 parent 91a8bd3 commit d17e5fb
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 523 deletions.
6 changes: 4 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"bracketSameLine": true,
"semi": true,
"singleQuote": true
"singleQuote": true,
"endOfLine": "lf",
"printWidth": 120
}
317 changes: 147 additions & 170 deletions lib/mixpanel/context.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,196 +2,173 @@
// @ts-nocheck test file contains web apis that are not available in node environment for typescript

import { describe, expect, test, vi } from 'vitest';
import {
fireEvent,
render,
renderHook,
RenderOptions,
} from '@testing-library/react';
import { fireEvent, render, renderHook, RenderOptions } from '@testing-library/react';
import React, { useEffect } from 'react';
import { MixpanelProvider, useMixpanelContext } from './context.tsx';
import { WebTrackingService } from './tracking/WebTrackingService.ts';

describe('MixpanelContext', () => {
const eventApiClient = vi.fn(() => Promise.resolve());

const ContextWrapper = ({
children,
defaultEventContext,
}: {
children: React.ReactNode;
defaultEventContext: MixpanelEvent;
}) => (
<MixpanelProvider
trackingService={new WebTrackingService(eventApiClient)}
defaultEventContext={defaultEventContext}
>
{children}
</MixpanelProvider>
);

const renderWithMixpanelProvider = (
ui: React.ReactElement,
options?: Omit<RenderOptions, 'wrapper'>
) => {
return render(ui, {
wrapper: (props) => (
<ContextWrapper {...props} {...options?.contextWrapperProps} />
),
...options?.testingLibraryOptions,
});
};

function TrackEventTestingComponent({
defaultEventContext,
}: {
defaultEventContext?: MixpanelEvent['context'];
}) {
const { trackEvent, setEventContext } = useMixpanelContext();

useEffect(() => {
if (defaultEventContext) {
setEventContext(defaultEventContext);
}
}, [defaultEventContext]);

return (
<button
onClick={() =>
trackEvent({
name: 'event name',
context: { title: 'Page title' },
data: { productId: '123' },
})
}
>
button
</button>
);
}

function TrackPageView() {
const { trackPageView } = useMixpanelContext();

useEffect(() => {
trackPageView({
data: {
title: 'Example',
pathname: '/product/1',
route: '/product/:id',
},
});
}, []);

return null;
}

test('provides expected context with trackEvent function', () => {
const { result } = renderHook(() => useMixpanelContext(), {
wrapper: ContextWrapper,
});

expect(result.current).toHaveProperty('trackEvent');
expect(typeof result.current.trackEvent).toBe('function');

expect(result.current).toHaveProperty('trackPageView');
expect(typeof result.current.trackPageView).toBe('function');
const eventApiClient = vi.fn(() => Promise.resolve());

const ContextWrapper = ({
children,
defaultEventContext,
}: {
children: React.ReactNode;
defaultEventContext: MixpanelEvent;
}) => (
<MixpanelProvider
trackingService={new WebTrackingService(eventApiClient)}
defaultEventContext={defaultEventContext}>
{children}
</MixpanelProvider>
);

const renderWithMixpanelProvider = (ui: React.ReactElement, options?: Omit<RenderOptions, 'wrapper'>) => {
return render(ui, {
wrapper: (props) => <ContextWrapper {...props} {...options?.contextWrapperProps} />,
...options?.testingLibraryOptions,
});
};

test('trackEvent sends correct data to api client', () => {
const { getByText } = renderWithMixpanelProvider(
<TrackEventTestingComponent />
);
function TrackEventTestingComponent({ defaultEventContext }: { defaultEventContext?: MixpanelEvent['context'] }) {
const { trackEvent, setEventContext } = useMixpanelContext();

fireEvent.click(getByText('button'));
useEffect(() => {
if (defaultEventContext) {
setEventContext(defaultEventContext);
}
}, [defaultEventContext]);

Check warning on line 41 in lib/mixpanel/context.test.tsx

View workflow job for this annotation

GitHub Actions / Run lint and tests

React Hook useEffect has a missing dependency: 'setEventContext'. Either include it or remove the dependency array

expect(eventApiClient).toHaveBeenCalledWith({
return (
<button
onClick={() =>
trackEvent({
name: 'event name',
context: {
title: 'Page title',
pathname: '/',
pwa: false,
},
data: {
productId: '123',
},
});
context: { title: 'Page title' },
data: { productId: '123' },
})
}>
button
</button>
);
}

function TrackPageView() {
const { trackPageView } = useMixpanelContext();

useEffect(() => {
trackPageView({
data: {
title: 'Example',
pathname: '/product/1',
route: '/product/:id',
},
});
}, []);

Check warning on line 68 in lib/mixpanel/context.test.tsx

View workflow job for this annotation

GitHub Actions / Run lint and tests

React Hook useEffect has a missing dependency: 'trackPageView'. Either include it or remove the dependency array

return null;
}

test('provides expected context with trackEvent function', () => {
const { result } = renderHook(() => useMixpanelContext(), {
wrapper: ContextWrapper,
});

test('provider can extend the default context for event tracking with provider prop', () => {
const defaultEventContext = {
href: 'https://example.com',
pathname: '/example',
audience: 'Consumer',
};
expect(result.current).toHaveProperty('trackEvent');
expect(typeof result.current.trackEvent).toBe('function');

const { getByText } = renderWithMixpanelProvider(
<TrackEventTestingComponent />,
{
contextWrapperProps: { defaultEventContext },
}
);
expect(result.current).toHaveProperty('trackPageView');
expect(typeof result.current.trackPageView).toBe('function');
});

fireEvent.click(getByText('button'));
test('trackEvent sends correct data to api client', () => {
const { getByText } = renderWithMixpanelProvider(<TrackEventTestingComponent />);

expect(eventApiClient).toHaveBeenCalledWith({
name: 'event name',
context: {
title: 'Page title',
href: 'https://example.com',
pathname: '/example',
pwa: false,
audience: 'Consumer',
},
data: {
productId: '123',
},
});
});
fireEvent.click(getByText('button'));

test('Default event context can be extended from a child component', () => {
const defaultEventContext = {
href: 'https://example.com',
pathname: '/example',
audience: 'Consumer',
};
expect(eventApiClient).toHaveBeenCalledWith({
name: 'event name',
context: {
title: 'Page title',
pathname: '/',
pwa: false,
},
data: {
productId: '123',
},
});
});

const { getByText } = renderWithMixpanelProvider(
<TrackEventTestingComponent
defaultEventContext={defaultEventContext}
/>
);
test('provider can extend the default context for event tracking with provider prop', () => {
const defaultEventContext = {
href: 'https://example.com',
pathname: '/example',
audience: 'Consumer',
};

fireEvent.click(getByText('button'));
const { getByText } = renderWithMixpanelProvider(<TrackEventTestingComponent />, {
contextWrapperProps: { defaultEventContext },
});

expect(eventApiClient).toHaveBeenCalledWith({
name: 'event name',
context: {
title: 'Page title',
href: 'https://example.com',
pathname: '/example',
pwa: false,
audience: 'Consumer',
},
data: {
productId: '123',
},
});
fireEvent.click(getByText('button'));

expect(eventApiClient).toHaveBeenCalledWith({
name: 'event name',
context: {
title: 'Page title',
href: 'https://example.com',
pathname: '/example',
pwa: false,
audience: 'Consumer',
},
data: {
productId: '123',
},
});
});

test('Default event context can be extended from a child component', () => {
const defaultEventContext = {
href: 'https://example.com',
pathname: '/example',
audience: 'Consumer',
};

const { getByText } = renderWithMixpanelProvider(
<TrackEventTestingComponent defaultEventContext={defaultEventContext} />
);

test('trackPageView sends correct data to api client', () => {
renderWithMixpanelProvider(<TrackPageView />);

expect(eventApiClient).toHaveBeenCalledWith({
name: 'Page view',
context: {
pwa: false,
},
data: {
title: 'Example',
pathname: '/product/1',
route: '/product/:id',
},
});
fireEvent.click(getByText('button'));

expect(eventApiClient).toHaveBeenCalledWith({
name: 'event name',
context: {
title: 'Page title',
href: 'https://example.com',
pathname: '/example',
pwa: false,
audience: 'Consumer',
},
data: {
productId: '123',
},
});
});

test('trackPageView sends correct data to api client', () => {
renderWithMixpanelProvider(<TrackPageView />);

expect(eventApiClient).toHaveBeenCalledWith({
name: 'Page view',
context: {
pwa: false,
},
data: {
title: 'Example',
pathname: '/product/1',
route: '/product/:id',
},
});
});
});
Loading

0 comments on commit d17e5fb

Please sign in to comment.