Skip to content

Commit

Permalink
Remove free in favor of demo (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
argaen authored Apr 19, 2024
1 parent 5d2356e commit 2022ca0
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 142 deletions.
16 changes: 0 additions & 16 deletions src/__tests__/app/user/login/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.
import * as auth0 from '@auth0/auth0-react';

import LoginPage from '@/app/user/login/page';
import * as helpers_env from '@/helpers/env';
import * as errors from '@/helpers/errors';

jest.mock('next/navigation');
Expand All @@ -19,13 +18,6 @@ jest.mock('@/lib/Stocker', () => ({
...jest.requireActual('@/lib/Stocker'),
}));

jest.mock('@/helpers/env', () => ({
__esModule: true,
get IS_DEMO_PLAN() {
return false;
},
}));

jest.mock('@/helpers/errors', () => ({
__esModule: true,
...jest.requireActual('@/helpers/errors'),
Expand All @@ -40,7 +32,6 @@ describe('LoginPage', () => {
push: mockRouterPush as AppRouterInstance['push'],
} as AppRouterInstance));

jest.spyOn(helpers_env, 'IS_DEMO_PLAN', 'get').mockReturnValue(false);
jest.spyOn(auth0, 'useAuth0').mockReturnValue({
isAuthenticated: false,
} as auth0.Auth0ContextInterface<auth0.User>);
Expand All @@ -55,13 +46,6 @@ describe('LoginPage', () => {
expect(mockRouterPush).toHaveBeenCalledWith('/dashboard/accounts');
});

it('sends to dashboard when staging', async () => {
jest.spyOn(helpers_env, 'IS_DEMO_PLAN', 'get').mockReturnValue(true);
render(<LoginPage />);

expect(mockRouterPush).toHaveBeenCalledWith('/dashboard/accounts');
});

it('shows loading... when not finished', () => {
const { container } = render(<LoginPage />);
expect(container).toMatchSnapshot();
Expand Down
20 changes: 0 additions & 20 deletions src/__tests__/hooks/useBookStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import useBookStorage from '@/hooks/useBookStorage';
import * as gapiHooks from '@/hooks/useGapiClient';
import BookStorage from '@/lib/storage/GDriveBookStorage';
import DemoBookStorage from '@/lib/storage/DemoBookStorage';
import { FreeBookStorage } from '@/lib/storage';
import * as helpers_env from '@/helpers/env';

jest.mock('@/hooks/useGapiClient', () => ({
Expand All @@ -22,9 +21,6 @@ jest.mock('@/helpers/env', () => ({
get IS_PAID_PLAN() {
return false;
},
get IS_FREE_PLAN() {
return false;
},
}));

describe('useBookStorage', () => {
Expand Down Expand Up @@ -73,22 +69,6 @@ describe('useBookStorage', () => {
});
});

it('returns FreeStorage when IS_FREE_PLAN', async () => {
jest.spyOn(helpers_env, 'IS_PAID_PLAN', 'get').mockReturnValue(false);
jest.spyOn(helpers_env, 'IS_FREE_PLAN', 'get').mockReturnValue(true);
window.gapi = {
client: {} as typeof gapi.client,
} as typeof gapi;
jest.spyOn(gapiHooks, 'default').mockReturnValue([true]);

const { result, rerender } = renderHook(() => useBookStorage());
rerender();

await waitFor(() => {
expect(result.current).toEqual({ storage: expect.any(FreeBookStorage) });
});
});

it('inits storage', async () => {
window.gapi = {
client: {} as typeof gapi.client,
Expand Down
18 changes: 0 additions & 18 deletions src/__tests__/hooks/useSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { renderHook } from '@testing-library/react';
import * as auth0 from '@auth0/auth0-react';

import useSession from '@/hooks/useSession';
import * as helpers_env from '@/helpers/env';

jest.mock('next/navigation');

Expand All @@ -13,14 +12,10 @@ jest.mock('@auth0/auth0-react', () => ({

jest.mock('@/helpers/env', () => ({
__esModule: true,
get IS_DEMO_PLAN() {
return false;
},
}));

describe('useSession', () => {
beforeEach(() => {
jest.spyOn(helpers_env, 'IS_DEMO_PLAN', 'get').mockReturnValue(false);
jest.spyOn(auth0, 'useAuth0').mockReturnValue({
isAuthenticated: false,
} as auth0.Auth0ContextInterface<auth0.User>);
Expand Down Expand Up @@ -53,17 +48,4 @@ describe('useSession', () => {
expect(result.current.accessToken).toEqual('accessToken');
expect(result.current.user).toEqual(user);
});

it('returns fake user when staging', async () => {
jest.spyOn(helpers_env, 'IS_DEMO_PLAN', 'get').mockReturnValue(true);
const { result } = renderHook(() => useSession());

expect(result.current.user).toEqual({
email: '[email protected]',
picture: '',
name: 'Maffin',
});
expect(result.current.isAuthenticated).toBe(true);
expect(result.current.isLoading).toBe(false);
});
});
13 changes: 13 additions & 0 deletions src/__tests__/lib/storage/DemoBookStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ describe('DemoBookStorage', () => {
);
expect(content).toEqual(pako.ungzip(rawBook));
});

it('returns empty data when in free.maffin.io', async () => {
Object.defineProperty(window, 'location', {
value: {
host: 'free.maffin.io',
},
writable: true,
});
const content = await instance.get();

expect(content).toEqual(new Uint8Array([]));
window.location.host = 'localhost';
});
});

describe('save', () => {
Expand Down
32 changes: 0 additions & 32 deletions src/__tests__/lib/storage/FreeBookStorage.test.ts

This file was deleted.

7 changes: 2 additions & 5 deletions src/app/user/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import React from 'react';
import { useRouter } from 'next/navigation';
import { useAuth0 } from '@auth0/auth0-react';

import { IS_DEMO_PLAN } from '@/helpers/env';
import { AuthError } from '@/helpers/errors';

export default function LoginPage(): JSX.Element {
const router = useRouter();
const { isAuthenticated, loginWithPopup, error } = useAuth0();

React.useEffect(() => {
if (IS_DEMO_PLAN || isAuthenticated) {
if (isAuthenticated) {
router.push('/dashboard/accounts');
}
}, [isAuthenticated, router]);
Expand All @@ -30,9 +29,7 @@ export default function LoginPage(): JSX.Element {
className="btn btn-primary"
type="button"
onClick={() => {
if (!IS_DEMO_PLAN) {
loginWithPopup();
}
loginWithPopup();
}}
>
Sign In
Expand Down
19 changes: 1 addition & 18 deletions src/helpers/env.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
export const IS_FREE_PLAN: boolean = (
process.env.NEXT_PUBLIC_ENV === 'staging'
&& typeof window !== 'undefined'
&& window.location.origin === 'https://free.maffin.io'
);
export const IS_PAID_PLAN: boolean = process.env.NEXT_PUBLIC_ENV === 'master';
export const IS_DEMO_PLAN: boolean = !IS_FREE_PLAN && !IS_PAID_PLAN;
export const IS_DEMO_PLAN = !IS_PAID_PLAN;

const PAID_PLAN_CONFIG = {
auth0: {
Expand All @@ -14,14 +9,6 @@ const PAID_PLAN_CONFIG = {
},
};

const FREE_PLAN_CONFIG = {
auth0: {
domain: 'maffin-dev.eu.auth0.com',
clientId: 'mMmnR4NbQOnim9B8QZfe9wfFuaKb8rwW',
scopes: 'profile email',
},
};

const DEMO_PLAN_CONFIG = {
auth0: {
domain: 'maffin-dev.eu.auth0.com',
Expand All @@ -35,10 +22,6 @@ function getConfig() {
return PAID_PLAN_CONFIG;
}

if (IS_FREE_PLAN) {
return FREE_PLAN_CONFIG;
}

return DEMO_PLAN_CONFIG;
}

Expand Down
7 changes: 1 addition & 6 deletions src/hooks/useBookStorage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react';

import useGapiClient from '@/hooks/useGapiClient';
import { IS_FREE_PLAN, IS_PAID_PLAN } from '@/helpers/env';
import { IS_PAID_PLAN } from '@/helpers/env';
import type BookStorage from '@/lib/storage/BookStorage';
import {
GDriveBookStorage,
DemoBookStorage,
FreeBookStorage,
} from '@/lib/storage';

type UseBookStorageReturn = {
Expand All @@ -26,10 +25,6 @@ export default function useBookStorage(): UseBookStorageReturn {
async function load() {
let instance: BookStorage = new DemoBookStorage();

if (IS_FREE_PLAN) {
instance = new FreeBookStorage();
}

if (IS_PAID_PLAN) {
instance = new GDriveBookStorage(window.gapi.client);
}
Expand Down
10 changes: 3 additions & 7 deletions src/hooks/useSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React from 'react';
import { useAuth0 } from '@auth0/auth0-react';
import type { User, Auth0ContextInterface } from '@auth0/auth0-react';

import { IS_DEMO_PLAN } from '@/helpers/env';

const emptyUser: User = {
name: '',
email: '',
Expand Down Expand Up @@ -39,10 +37,8 @@ export default function useSession(): SessionReturn {
return {
...auth0,
accessToken,
isAuthenticated: IS_DEMO_PLAN ? true : auth0.isAuthenticated,
isLoading: IS_DEMO_PLAN ? false : auth0.isLoading,
user: IS_DEMO_PLAN
? { name: 'Maffin', email: '[email protected]', picture: '' }
: auth0.user || emptyUser,
isAuthenticated: auth0.isAuthenticated,
isLoading: auth0.isLoading,
user: auth0.user || emptyUser,
};
}
4 changes: 4 additions & 0 deletions src/lib/storage/DemoBookStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export default class DemoBookStorage implements BookStorage {
}

async get(): Promise<Uint8Array> {
if (window.location.host === 'free.maffin.io') {
return new Uint8Array([]);
}

const response = await fetch(
`/books/${this.fileName}.sqlite.gz`,
{
Expand Down
19 changes: 0 additions & 19 deletions src/lib/storage/FreeBookStorage.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as GDriveBookStorage } from './GDriveBookStorage';
export { default as DemoBookStorage } from './DemoBookStorage';
export { default as FreeBookStorage } from './FreeBookStorage';

0 comments on commit 2022ca0

Please sign in to comment.