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

Vue test utils not updating template when testing composables #2089

Open
EbenOladutemu opened this issue Jan 9, 2024 · 0 comments
Open

Vue test utils not updating template when testing composables #2089

EbenOladutemu opened this issue Jan 9, 2024 · 0 comments

Comments

@EbenOladutemu
Copy link

EbenOladutemu commented Jan 9, 2024

Subject of the issue

I have a simple test.

I expect the value of isEditing to be false initially. Then when I click on the edit button, I now expect isEditing to be true.

describe('LSA Edit', () => {
  test('Edit button is clickable', async () => {
    const TestComponent = defineComponent({
      setup() {
        return {
          ...useLoadsheddingComposable()
        }
      }
    })

    const wrapper = mount(TestComponent, {
      global: {
        plugins: [
          createTestingPinia({
            initialState: {
              loadshedding: {
                selectedSuburb: 'Province 3'
              }
            }
          })
        ]
      }
    })

    expect(wrapper.vm.isEditing).toBe(false)

    const wrapperSuburb = mount(SuburbSearch)
    const editSuburb = wrapperSuburb.find('[data-test="edit-suburb"]')

    await editSuburb.trigger('click')

    expect(wrapper.vm.isEditing).toBe(true)
  })
})

This is my template

  <div class="lsa-container" v-if="!isEditing">
    <div class="lsa-suburb">
      <p class="lsa-suburb__zone">
        {{ selectedSuburb.name }} (Zone {{ selectedSuburb.block_number }})
      </p>
      <p class="lsa-suburb__muni">
        {{ selectedSuburb.municipality }}, {{ selectedSuburb.province }}
      </p>
    </div>
    <div class="lsa-options">
      <UtilIcon :icon="'edit'" @click="editSuburb" data-test="edit-suburb" />
      <UtilIcon :icon="'delete'" @click="deleteSuburb" />
    </div>
  </div>
    
   <div v-if="isEditing">
      <input
        type="text"
        placeholder="Add area"
        @input="getSuburbs()"
        v-model="searchQuery"
        data-test="suburb-input"
      />
      <ul v-show="searchQuery.length > 0 && filteredSuburbs.length > 0">
        <li
          v-for="suburb in filteredSuburbs"
          :key="suburb.id"
          @click="chooseSuburb(suburb)"
          data-test="suburb-list"
        >
          <span>{{ suburb.name }} (Zone {{ suburb.block_number }})</span> <br />
          <strong>
            <span>{{ suburb.municipality }}, {{ suburb.province }}</span>
          </strong>
        </li>
      </ul>
      <p v-if="searchQuery.length > 2 && filteredSuburbs.length == 0">
        No area found
      </p>
    </div>

This is my composable

export function useLoadsheddingComposable() {
  const isEditing = ref(false)

  const editSuburb = () => {
    console.log('Edit suburb clicked')
    isEditing.value = true
  }

  return {
    isEditing,
    editSuburb
  }
}

The first assertion passed and it's false but the second fails as I am still getting Expected: true

Anything I'm doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant