Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show subscription buttons in simple view, add ability to hide rss button #1378

Merged
merged 1 commit into from
Jun 6, 2022
Merged
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
3 changes: 0 additions & 3 deletions frontend/app/__stubs__/settings.ts

This file was deleted.

19 changes: 0 additions & 19 deletions frontend/app/common/__mocks__/settings.ts

This file was deleted.

48 changes: 19 additions & 29 deletions frontend/app/common/settings.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
import { parseQuery } from 'utils/parse-query';

import type { Theme } from './types';
import { THEMES, MAX_SHOWN_ROOT_COMMENTS } from './constants';

export interface QuerySettingsType {
site_id?: string;
page_title?: string;
url?: string;
max_shown_comments?: number;
theme: Theme;
/* used in delete users data page */
token?: string;
show_email_subscription?: boolean;
}
function parseNumber(value: unknown) {
if (typeof value !== 'string') {
return undefined;
}

export const querySettings: Partial<QuerySettingsType> = parseQuery();
const parsed = +value;

if (querySettings.max_shown_comments) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
querySettings.max_shown_comments = parseInt(querySettings.max_shown_comments as any as string, 10);
} else {
querySettings.max_shown_comments = MAX_SHOWN_ROOT_COMMENTS;
return isNaN(parsed) ? undefined : parsed;
}

if (!querySettings.theme || THEMES.indexOf(querySettings.theme) === -1) {
querySettings.theme = THEMES[0];
function includes<T extends U, U>(coll: ReadonlyArray<T>, el: U): el is T {
return coll.includes(el as T);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
querySettings.show_email_subscription = (querySettings.show_email_subscription as any) !== 'false';

export const siteId = querySettings.site_id!;
export const pageTitle = querySettings.page_title;
export const url = querySettings.url;
export const maxShownComments = querySettings.max_shown_comments;
export const token = querySettings.token!;
export const theme = querySettings.theme;
export const rawParams = parseQuery();
export const maxShownComments = parseNumber(rawParams.max_shown_comments) ?? MAX_SHOWN_ROOT_COMMENTS;
export const isEmailSubscription = rawParams.show_email_subscription !== 'false';
export const isRssSubscription =
rawParams.show_rss_subscription === undefined || rawParams.show_rss_subscription !== 'false';
export const theme = (rawParams.theme = includes(THEMES, rawParams.theme) ? rawParams.theme : THEMES[0]);
export const siteId = rawParams.site_id || 'remark';
export const pageTitle = rawParams.page_title;
export const url = rawParams.url;
export const token = rawParams.token;
export const locale = rawParams.locale || 'en';
3 changes: 0 additions & 3 deletions frontend/app/common/static-store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Config } from './types';
import { QuerySettingsType, querySettings } from './settings';

interface StaticStoreType {
config: Config;
query: QuerySettingsType;
/** used in fetcher, fer example to set comment edit timeout */
serverClientTimeDiff?: number;
}
Expand Down Expand Up @@ -32,5 +30,4 @@ export const StaticStore: StaticStoreType = {
telegram_bot_username: '',
emoji_enabled: false,
},
query: querySettings as QuerySettingsType,
};
157 changes: 157 additions & 0 deletions frontend/app/components/comment-form/comment-form.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import '@testing-library/jest-dom';
import { fireEvent, screen, waitFor } from '@testing-library/preact';
import { useIntl } from 'react-intl';

import { render } from 'tests/utils';
import { StaticStore } from 'common/static-store';
import { LS_SAVED_COMMENT_VALUE } from 'common/constants';
import * as localStorageModule from 'common/local-storage';

import { CommentForm, CommentFormProps, messages } from './comment-form';

const user: CommentFormProps['user'] = {
name: 'username',
id: 'id_1',
picture: '',
ip: '',
admin: false,
block: false,
verified: false,
};

function setup(
overrideProps: Partial<CommentFormProps> = {},
overrideConfig: Partial<typeof StaticStore['config']> = {}
) {
Object.assign(StaticStore.config, overrideConfig);

const props = {
mode: 'main',
theme: 'light',
onSubmit: () => Promise.resolve(),
getPreview: () => Promise.resolve(''),
user: null,
id: '1',
...overrideProps,
} as CommentFormProps;
const CommentFormWithIntl = () => <CommentForm {...props} intl={useIntl()} />;

return render(<CommentFormWithIntl />);
}
describe('<CommentForm />', () => {
afterEach(() => {
// reset textarea id in order to have `textarea_1` for every test
CommentForm.textareaId = 0;
});

describe('with initial comment value', () => {
afterEach(() => {
localStorage.clear();
});
it('should has empty value', () => {
const value = 'text';

localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: value }));
setup();
expect(screen.getByTestId('textarea_1')).toHaveValue(value);
});

it('should get initial value from localStorage', () => {
const value = 'text';

localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: value }));
setup();
expect(screen.getByTestId('textarea_1')).toHaveValue(value);
});
it('should get initial value from props instead localStorage', () => {
const value = 'text from props';

localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: 'text from localStorage' }));

setup({ value });
expect(screen.getByTestId('textarea_1')).toHaveValue(value);
});
});

describe('update initial value', () => {
afterEach(() => {
localStorage.clear();
});
it('should update value', () => {
setup();

fireEvent.input(screen.getByTestId('textarea_1'), { target: { value: '1' } });
expect(localStorage.getItem(LS_SAVED_COMMENT_VALUE)).toBe('{"1":"1"}');

fireEvent.input(screen.getByTestId('textarea_1'), { target: { value: '11' } });
expect(localStorage.getItem(LS_SAVED_COMMENT_VALUE)).toBe('{"1":"11"}');
});

it('should clear value after send', async () => {
localStorage.setItem(LS_SAVED_COMMENT_VALUE, JSON.stringify({ 1: 'asd' }));
const updateJsonItemSpy = jest.spyOn(localStorageModule, 'updateJsonItem');

setup();
fireEvent.submit(screen.getByTestId('textarea_1'));
await waitFor(() => {
expect(updateJsonItemSpy).toHaveBeenCalled();
});
expect(localStorage.getItem(LS_SAVED_COMMENT_VALUE)).toBe('{}');
});
});

it(`doesn't render preview button and markdown toolbar in simple mode`, () => {
setup({ user }, { simple_view: true });
expect(screen.queryByTestId('markdown-toolbar')).not.toBeInTheDocument();
expect(screen.queryByText('Preview')).not.toBeInTheDocument();
});

it.each`
expected | value
${'99'} | ${'That was Wintermute, manipulating the lock the way it had manipulated the drone micro and the chassis of a gutted game console. It was chambered for .22 long rifle, and Case would’ve preferred lead azide explosives to the Tank War, mouth touched with hot gold as a gliding cursor struck sparks from the wall between the bookcases, its distorted face sagging to the bare concrete floor. Splayed in his elastic g-web, Case watched the other passengers as he made his way down Shiga from the sushi stall he cradled it in his jacket pocket. Images formed and reformed: a flickering montage of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the Japanese night like live wire voodoo and he’d cry for it, cry in his jacket pocket. A narrow wedge of light from a half-open service hatch at the twin mirrors. Still it was a square of faint light. The alarm still oscillated, louder here, the rear wall dulling the roar of the arcade showed him broken lengths of damp chipboard and the robot gardener. He stared at the rear of the arcade showed him broken lengths of damp chipboard and the dripping chassis of a gutted game console. That was Wintermute, manipulating the lock the way it had manipulated the drone micro and the chassis of a gutted game console. It was chambered for .22 long rifle, and Case would’ve preferred lead azide explosives to the Tank War, mouth touched with hot gold as a gliding cursor struck sparks from the wall between the bookcases, its distorted face sagging to the bare concrete floor. Splayed in his elastic g-web, Case watched the other passengers as he made his way down Shiga from the sushi stall he cradled it in his jacket pocket. Images formed and reformed: a flickering montage of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the Japanese night like live wire voodoo and he’d cry for it, cry in his jacket.'}
${'0'} | ${'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestib'}
${'-425'} | ${'All the speed he took, all the turns he’d taken and the amplified breathing of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the dark. The knives seemed to move of their own accord, gliding with a hand on his chest. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the Flatline as a construct, a hardwired ROM cassette replicating a dead man’s skills, obsessions, kneejerk responses. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the bright void beyond the chain link. Now this quiet courtyard, Sunday afternoon, this girl with a random collection of European furniture, as though Deane had once intended to use the place as his home. Now this quiet courtyard, Sunday afternoon, this girl with a ritual lack of urgency through the arcs and passes of their dance, point passing point, as the men waited for an opening. They floated in the shade beneath a bridge or overpass. A graphic representation of data abstracted from the banks of every computer in the coffin for Armitage’s call. All the speed he took, all the turns he’d taken and the amplified breathing of the Sprawl’s towers and ragged Fuller domes, dim figures moving toward him in the dark. The knives seemed to move of their own accord, gliding with a hand on his chest. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the Flatline as a construct, a hardwired ROM cassette replicating a dead man’s skills, obsessions, kneejerk responses. Case had never seen him wear the same suit twice, although his wardrobe seemed to consist entirely of meticulous reconstruction’s of garments of the bright void beyond the chain link. Now this quiet courtyard, Sunday afternoon, this girl with a random collection of European furniture, as though Deane had once intended to use the place as his home. Now this quiet courtyard, Sunday afternoon, this girl with a ritual lack of urgency through the arcs and passes of their dance, point passing point, as the men waited for an opening. They floated in the shade beneath a bridge or overpass. A graphic representation of data abstracted from the banks of every computer in the coffin for Armitage’s call.'}
`('renders counter of rest symbols', async ({ value, expected }) => {
setup({ value }, { max_comment_size: 2000 });
expect(screen.getByText(expected)).toBeInTheDocument();
});

describe('when authorized', () => {
describe('with simple view', () => {
it('renders email subscription button', () => {
setup({ user }, { simple_view: true, email_notifications: true });
expect(screen.getByText(/Subscribe by/)).toBeVisible();
expect(screen.getByTitle('Subscribe by Email')).toBeVisible();
});
it('renders rss subscription button', () => {
setup({ user }, { simple_view: true });
expect(screen.getByText(/Subscribe by/)).toBeVisible();
expect(screen.getByTitle('Subscribe by RSS')).toBeVisible();
});
});
it('renders without email subscription button when email_notifications disabled', () => {
setup({ user }, { email_notifications: false });
expect(screen.queryByText('Subscribe by RSS')).not.toBeInTheDocument();
});
});

describe('when unauthorized', () => {
it(`doesn't email subscription button`, () => {
setup();
expect(screen.queryByText(/Subscribe by/)).not.toBeInTheDocument();
expect(screen.queryByTitle('Subscribe bey Email')).not.toBeInTheDocument();
});

it(`doesn't render rss subscription button`, () => {
setup();
expect(screen.queryByText(/Subscribe by/)).not.toBeInTheDocument();
expect(screen.queryByText('Subscribe by RSS')).not.toBeInTheDocument();
});

it('should show error message of image upload try by anonymous user', () => {
setup({ user: { ...user, id: 'anonymous_1' } });
fireEvent.drop(screen.getByTestId('commentform_1'));
expect(screen.getByText(messages.anonymousUploadingDisabled.defaultMessage)).toBeInTheDocument();
});
});
});
Loading