Skip to content
Snippets Groups Projects

Test filter by followed by

Closed Hannes Heine requested to merge pr1542head into pr1542base

Created by: Tirokk

aonomike Authored by aonomike Merged


:cake: Pullrequest

  • The filterBubble.js file no longer exists hence tests to check that posts can be filtered by user followed have been moved to posts.spec.js
  • Refactor variables used in the tests to have better names

Issues

Todo

  • None

Merge request reports

Closed by avatar (Jul 4, 2025 6:33pm UTC)

Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
124 114 })
125 115
116 describe('no filter', () => {
117 it('returns all posts', async () => {
118 const postQueryNoFilters = gql`
119 query Post($filter: _PostFilter) {
120 Post(filter: $filter) {
121 id
122 }
123 }
124 `
125 const expected = [{ id: 'happy-post' }, { id: 'cry-post' }, { id: 'post-by-followed-user' }]
126 variables = { filter: {} }
127 await expect(query({ query: postQueryNoFilters, variables })).resolves.toMatchObject({
128 data: {
129 Post: expect.arrayContaining(expected),
  • Created by: Tirokk

    roschaefer Authored by roschaefer


    Outdated (history rewrite) - original diff


    @@ -91,44 +91,63 @@ afterEach(async () => {
     })
     
     describe('Post', () => {
    -  const postQueryFilteredByCategories = gql`
    -    query Post($filter: _PostFilter) {
    -      Post(filter: $filter) {
    -        id
    -        categories {
    -          id
    -        }
    -      }
    -    }
    -  `
    -
    -  const postQueryFilteredByEmotions = gql`
    -    query Post($filter: _PostFilter) {
    -      Post(filter: $filter) {
    -        id
    -        emotions {
    -          emotion
    -        }
    -      }
    -    }
    -  `
    -
       describe('can be filtered', () => {
    -    let post31, post32
    +    let followedUser, happyPost, cryPost
         beforeEach(async () => {
    -      ;[post31, post32] = await Promise.all([
    -        factory.create('Post', { id: 'p31', categoryIds: ['cat4'] }),
    -        factory.create('Post', { id: 'p32', categoryIds: ['cat15'] }),
    -        factory.create('Post', { id: 'p33', categoryIds: ['cat9'] }),
    +      ;[followedUser] = await Promise.all([
    +        factory.create('User', {
    +          id: 'followed-by-me',
    +          email: 'followed@example.org',
    +          name: 'Followed User',
    +          password: '1234',
    +        }),
    +      ])
    +      ;[happyPost, cryPost] = await Promise.all([
    +        factory.create('Post', { id: 'happy-post', categoryIds: ['cat4'] }),
    +        factory.create('Post', { id: 'cry-post', categoryIds: ['cat15'] }),
    +        factory.create('Post', {
    +          id: 'post-by-followed-user',
    +          categoryIds: ['cat9'],
    +          author: followedUser,
    +        }),
           ])
         })
     
    +    describe('no filter', () => {
    +      it('returns all posts', async () => {
    +        const postQueryNoFilters = gql`
    +          query Post($filter: _PostFilter) {
    +            Post(filter: $filter) {
    +              id
    +            }
    +          }
    +        `
    +        const expected = [{ id: 'happy-post' }, { id: 'cry-post' }, { id: 'post-by-followed-user' }]
    +        variables = { filter: {} }
    +        await expect(query({ query: postQueryNoFilters, variables })).resolves.toMatchObject({
    +          data: {
    +            Post: expect.arrayContaining(expected),

    If you use toMatchObject you don't need the expect.arrayContaining unless you expect even more items than the three.

  • Created by: Tirokk

    mattwr18 Authored by mattwr18


    Actually, we started off by not using arrayContaining and the test failed because it returned the posts in a different order than our expected array, so we refactored it to say the order is not important.

  • Created by: Tirokk

    roschaefer Authored by roschaefer


    You could orderBy in the query? It does not really matter. I like your approach, too! :thumbsup:

  • Hannes Heine
    Hannes Heine @Elweyn started a thread on commit 785b59f9
  • 109 id: 'post-by-followed-user',
    110 categoryIds: ['cat9'],
    111 author: followedUser,
    112 }),
    123 113 ])
    124 114 })
    125 115
    116 describe('no filter', () => {
    117 it('returns all posts', async () => {
    118 const postQueryNoFilters = gql`
    119 query Post($filter: _PostFilter) {
    120 Post(filter: $filter) {
    121 id
    122 }
    123 }
    124 `
    • Created by: Tirokk

      roschaefer Authored by roschaefer


      Outdated (history rewrite) - original diff


      @@ -91,44 +91,63 @@ afterEach(async () => {
       })
       
       describe('Post', () => {
      -  const postQueryFilteredByCategories = gql`
      -    query Post($filter: _PostFilter) {
      -      Post(filter: $filter) {
      -        id
      -        categories {
      -          id
      -        }
      -      }
      -    }
      -  `
      -
      -  const postQueryFilteredByEmotions = gql`
      -    query Post($filter: _PostFilter) {
      -      Post(filter: $filter) {
      -        id
      -        emotions {
      -          emotion
      -        }
      -      }
      -    }
      -  `
      -
         describe('can be filtered', () => {
      -    let post31, post32
      +    let followedUser, happyPost, cryPost
           beforeEach(async () => {
      -      ;[post31, post32] = await Promise.all([
      -        factory.create('Post', { id: 'p31', categoryIds: ['cat4'] }),
      -        factory.create('Post', { id: 'p32', categoryIds: ['cat15'] }),
      -        factory.create('Post', { id: 'p33', categoryIds: ['cat9'] }),
      +      ;[followedUser] = await Promise.all([
      +        factory.create('User', {
      +          id: 'followed-by-me',
      +          email: 'followed@example.org',
      +          name: 'Followed User',
      +          password: '1234',
      +        }),
      +      ])
      +      ;[happyPost, cryPost] = await Promise.all([
      +        factory.create('Post', { id: 'happy-post', categoryIds: ['cat4'] }),
      +        factory.create('Post', { id: 'cry-post', categoryIds: ['cat15'] }),
      +        factory.create('Post', {
      +          id: 'post-by-followed-user',
      +          categoryIds: ['cat9'],
      +          author: followedUser,
      +        }),
             ])
           })
       
      +    describe('no filter', () => {
      +      it('returns all posts', async () => {
      +        const postQueryNoFilters = gql`
      +          query Post($filter: _PostFilter) {
      +            Post(filter: $filter) {
      +              id
      +            }
      +          }
      +        `

      I believe it helps to avoid false negatives (test does not fail although there is a bug) if you use the very same graphql query throughout the scope. await exect(...).resolves.toMatchObject(...) should be lenient enough about any additional data in the actual response.

  • Hannes Heine
    Hannes Heine @Elweyn started a thread on commit 785b59f9
  • 174 it('filters by single emotion', async () => {
    175 const expected = {
    176 data: {
    177 Post: [
    178 {
    179 id: 'happy-post',
    180 emotions: [{ emotion: 'happy' }],
    181 },
    182 ],
    183 },
    184 }
    185 await user.relateTo(happyPost, 'emoted', { emotion: 'happy' })
    186 variables = { ...variables, filter: { emotions_some: { emotion_in: ['happy'] } } }
    187 await expect(
    188 query({ query: postQueryFilteredByEmotions, variables }),
    189 ).resolves.toMatchObject(expected)
    • Created by: Tirokk

      roschaefer Authored by roschaefer


      Outdated (history rewrite) - original diff


      @@ -140,45 +159,88 @@ describe('Post', () => {
             ).resolves.toMatchObject(expected)
           })
       
      -    it('by emotions', async () => {
      +    describe('by emotions', () => {
      +      const postQueryFilteredByEmotions = gql`
      +        query Post($filter: _PostFilter) {
      +          Post(filter: $filter) {
      +            id
      +            emotions {
      +              emotion
      +            }
      +          }
      +        }
      +      `
      +
      +      it('filters by single emotion', async () => {
      +        const expected = {
      +          data: {
      +            Post: [
      +              {
      +                id: 'happy-post',
      +                emotions: [{ emotion: 'happy' }],
      +              },
      +            ],
      +          },
      +        }
      +        await user.relateTo(happyPost, 'emoted', { emotion: 'happy' })
      +        variables = { ...variables, filter: { emotions_some: { emotion_in: ['happy'] } } }
      +        await expect(
      +          query({ query: postQueryFilteredByEmotions, variables }),
      +        ).resolves.toMatchObject(expected)

      Here you omit the expect.arrayContaining :thumbsup:

    • Created by: Tirokk

      mattwr18 Authored by mattwr18


      yes, we wanted to use this for the multiple emotions test as well, but it failed as I stated :point_up:

  • Created by: Mogge

    Review: Approved

    exercise

    It's a great exercise to get familiar with the code by writing tests! You have my approval for this PR but if you want to, here are some suggestions to make this even better.

    FYI: @ogerly along with writing documentation (you did that with your video tutorial) writing a test is a good way to learn :point_up:

  • Please register or sign in to reply
    Loading