import { t } from '@lingui/macro'; import { getAllSubjects } from '@services/graphql/queries'; import Link from 'next/link'; import useSWR from 'swr'; import styles from './TopicsList.module.scss'; const TopicsList = () => { const { data, error } = useSWR('/api/subjects', getAllSubjects); if (error) return
{t`Failed to load.`}
; if (!data) return
{t`Loading...`}
; const sortedSubjects = [...data].sort((a, b) => a.title.localeCompare(b.title) ); const subjects = sortedSubjects.map((subject) => { return (
  • {subject.title}
  • ); }); return (

    {t`Topics`}

    ); }; export default TopicsList; : www.armandphilippot.com
    The frontend of my personal website.Armand Philippot
    aboutsummaryrefslogtreecommitdiffstats
    path: root/src/components/organisms/comments-list/comments-list.test.tsx
    blob: 8feb97c26002bd27b523f1e299bde8a840269729 (plain)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    
    import { describe, expect, it } from '@jest/globals';
    import { userEvent } from '@testing-library/user-event';
    import { render, screen as rtlScreen } from '../../../../tests/utils';
    import { type CommentData, CommentsList } from './comments-list';
    
    describe('CommentsList', () => {
      it('renders a list of approved comments', () => {
        const comments = [
          {
            author: { name: 'Milan0' },
            content: 'Fugit veniam quas qui dolor explicabo.',
            id: 1,
            isApproved: true,
            publicationDate: '2023-01-23',
          },
          {
            author: { name: 'Haskell42' },
            content: 'Error quas accusamus nesciunt enim quae a.',
            id: 2,
            isApproved: true,
            publicationDate: '2023-02-04',
          },
        ] satisfies CommentData[];
    
        render(<CommentsList comments={comments} />);
    
        expect(rtlScreen.getAllByRole('listitem')).toHaveLength(comments.length);
        expect(rtlScreen.getByText(comments[0].author.name)).toBeInTheDocument();
        expect(rtlScreen.getByText(comments[0].content)).toBeInTheDocument();
        expect(rtlScreen.getByText(comments[1].author.name)).toBeInTheDocument();
        expect(rtlScreen.getByText(comments[1].content)).toBeInTheDocument();
      });
    
      it('renders a list of pending comments', () => {
        const comments = [
          {
            author: { name: 'Milan0' },
            content: 'Fugit veniam quas qui dolor explicabo.',
            id: 1,
            isApproved: false,
            publicationDate: '2023-01-23',
          },
          {
            author: { name: 'Haskell42' },
            content: 'Error quas accusamus nesciunt enim quae a.',
            id: 2,
            isApproved: false,
            publicationDate: '2023-02-04',
          },
        ] satisfies CommentData[];
    
        render(<CommentsList comments={comments} />);
    
        expect(rtlScreen.getAllByRole('listitem')).toHaveLength(comments.length);
        expect(
          rtlScreen.queryByText(comments[0].author.name)
        ).not.toBeInTheDocument();
        expect(rtlScreen.queryByText(comments[0].content)).not.toBeInTheDocument();
        expect(
          rtlScreen.queryByText(comments[1].author.name)
        ).not.toBeInTheDocument();
        expect(rtlScreen.queryByText(comments[1].content)).not.toBeInTheDocument();
      });
    
      it('renders a mixed list of approved and pending comments', () => {
        const comments = [
          {
            author: { name: 'Milan0' },
            content: 'Fugit veniam quas qui dolor explicabo.',
            id: 1,
            isApproved: true,
            publicationDate: '2023-01-23',
          },
          {
            author: { name: 'Haskell42' },
            content: 'Error quas accusamus nesciunt enim quae a.',
            id: 2,
            isApproved: false,
            publicationDate: '2023-02-04',
          },
        ] satisfies CommentData[];
    
        render(<CommentsList comments={comments} />);
    
        expect(rtlScreen.getAllByRole('listitem')).toHaveLength(comments.length);
        expect(rtlScreen.getByText(comments[0].author.name)).toBeInTheDocument();
        expect(rtlScreen.getByText(comments[0].content)).toBeInTheDocument();
        expect(
          rtlScreen.queryByText(comments[1].author.name)
        ).not.toBeInTheDocument();
        expect(rtlScreen.queryByText(comments[1].content)).not.toBeInTheDocument();
      });
    
      it('does not render the replies by default', () => {
        const comments = [
          {
            author: { name: 'Milan0' },
            content: 'Fugit veniam quas qui dolor explicabo.',
            id: 1,
            isApproved: true,
            publicationDate: '2023-01-23',
            replies: [
              {
                author: { name: 'Haskell42' },
                content: 'Error quas accusamus nesciunt enim quae a.',
                id: 2,
                isApproved: true,
                publicationDate: '2023-02-04',
              },
              {
                author: { name: 'Hanna49' },
                content:
                  'Ut ducimus neque aliquam soluta sed totam commodi cum sit.',
                id: 3,
                isApproved: true,
                publicationDate: '2023-03-10',
              },
            ],
          },
        ] satisfies CommentData[];
    
        render(<CommentsList comments={comments} />);
    
        expect(rtlScreen.getAllByRole('listitem')).toHaveLength(1);
        expect(rtlScreen.getByText(comments[0].author.name)).toBeInTheDocument();
        expect(rtlScreen.getByText(comments[0].content)).toBeInTheDocument();
        expect(
          rtlScreen.queryByText(comments[0].replies[0].author.name)
        ).not.toBeInTheDocument();
        expect(
          rtlScreen.queryByText(comments[0].replies[0].content)
        ).not.toBeInTheDocument();
        expect(rtlScreen.queryByText(/Reply/)).not.toBeInTheDocument();
      });
    
      it('can render the replies by providing a depth', () => {
        const comments = [
          {
            author: { name: 'Milan0' },
            content: 'Fugit veniam quas qui dolor explicabo.',
            id: 1,
            isApproved: true,
            publicationDate: '2023-01-23',
            replies: [
              {
                author: { name: 'Haskell42' },
                content: 'Error quas accusamus nesciunt enim quae a.',
                id: 2,
                isApproved: true,
                publicationDate: '2023-02-04',
              },
              {
                author: { name: 'Hanna49' },
                content:
                  'Ut ducimus neque aliquam soluta sed totam commodi cum sit.',
                id: 3,
                isApproved: true,
                publicationDate: '2023-03-10',
              },
            ],
          },
        ] satisfies CommentData[];
    
        render(<CommentsList comments={comments} depth={1} />);
    
        const totalComments =
          comments.length +
          comments.reduce(
            (accumulator, currentValue) =>
              accumulator + currentValue.replies.length,
            0
          );
    
        expect(rtlScreen.getAllByRole('listitem')).toHaveLength(totalComments);
        expect(rtlScreen.getByText(comments[0].author.name)).toBeInTheDocument();
        expect(rtlScreen.getByText(comments[0].content)).toBeInTheDocument();
        expect(
          rtlScreen.getByText(comments[0].replies[0].author.name)
        ).toBeInTheDocument();
        expect(
          rtlScreen.getByText(comments[0].replies[0].content)
        ).toBeInTheDocument();
        expect(rtlScreen.getByText(/Reply/)).toBeInTheDocument();
      });
    
      it('can allow replies on replies by providing a depth', () => {
        const comments = [
          {
            author: { name: 'Milan0' },
            content: 'Fugit veniam quas qui dolor explicabo.',
            id: 1,
            isApproved: true,
            publicationDate: '2023-01-23',
            replies: [
              {
                author: { name: 'Haskell42' },
                content: 'Error quas accusamus nesciunt enim quae a.',
                id: 2,
                isApproved: true,
                publicationDate: '2023-02-04',
              },
              {
                author: { name: 'Hanna49' },
                content:
                  'Ut ducimus neque aliquam soluta sed totam commodi cum sit.',
                id: 3,
                isApproved: true,
                publicationDate: '2023-03-10',
              },
            ],
          },
        ] satisfies CommentData[];
    
        render(<CommentsList comments={comments} depth={3} />);
    
        const totalComments =
          comments.length +
          comments.reduce(
            (accumulator, currentValue) =>
              accumulator + currentValue.replies.length,
            0
          );
    
        expect(rtlScreen.getAllByRole('listitem')).toHaveLength(totalComments);
        expect(rtlScreen.getAllByText(/Reply/)).toHaveLength(totalComments);
      });
    
      it('can render a reply form when clicking on the reply button', async () => {
        const user = userEvent.setup();
        const comments = [
          {
            author: { name: 'Milan0' },
            content: 'Fugit veniam quas qui dolor explicabo.',
            id: 1,
            isApproved: true,
            publicationDate: '2023-01-23',
          },
          {
            author: { name: 'Haskell42' },
            content: 'Error quas accusamus nesciunt enim quae a.',
            id: 2,
            isApproved: true,
            publicationDate: '2023-02-04',
          },
        ] satisfies CommentData[];
    
        render(<CommentsList comments={comments} depth={2} />);
    
        // eslint-disable-next-line @typescript-eslint/no-magic-numbers
        expect.assertions(3);
    
        const replyButtons = rtlScreen.getAllByText(/Reply/);
    
        expect(rtlScreen.queryByRole('form')).not.toBeInTheDocument();
    
        await user.click(replyButtons[0]);
    
        expect(rtlScreen.getByRole('form')).toHaveAccessibleName(/Leave a reply/);
    
        await user.click(replyButtons[0]);
    
        expect(rtlScreen.queryByRole('form')).not.toBeInTheDocument();
      });
    
      it('does not render a reply button when replies are forbidden', () => {
        const comments = [
          {
            author: { name: 'Milan0' },
            content: 'Fugit veniam quas qui dolor explicabo.',
            id: 1,
            isApproved: true,
            publicationDate: '2023-01-23',
          },
          {
            author: { name: 'Haskell42' },
            content: 'Error quas accusamus nesciunt enim quae a.',
            id: 2,
            isApproved: true,
            publicationDate: '2023-02-04',
          },
        ] satisfies CommentData[];
    
        render(<CommentsList areRepliesForbidden comments={comments} depth={2} />);
    
        expect(rtlScreen.queryAllByRole('button', { name: /Reply/ })).toHaveLength(
          0
        );
        expect(rtlScreen.queryByRole('form')).not.toBeInTheDocument();
      });
    });