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

using <template> in tests, how to rerender with an updated parameter #189

Open
cah-brian-gantzler opened this issue Aug 14, 2023 · 4 comments

Comments

@cah-brian-gantzler
Copy link

In tests, when using hbs, you can update a param with set, rerender and get the change.
given as TestComponent2

<template>
  {{#if @param}}
    hello
  {{/if}}
</template>

you can do this test and it works.

  test('it renders hbs', async function (assert) {
    this.param = false;

    await render(hbs`<TestComponent2 @param={{this.param}}/>`);

    assert.dom(this.element).hasText('');

    set(this, "param", true);

    await rerender();   // can also use settled
    assert.dom(this.element).hasText('hello');
  });

Since the context is changed for gjs, the following test does not work.

  test('it renders gjs', async function (assert) {
    let param = false;

    await render(<template><TestComponent2 @param={{param}}/></template>);

    assert.dom(this.element).hasText('');

    param = true;

    await rerender();
    assert.dom(this.element).hasText('hello');
  });

Cant really use set or notifyPropertyChange as the param is not on an object to pass. One way I see is to create a class with param tracked but that seems overkill for tests from what we had before.

What is the recommended way to write tests that update params when using <template> in tests?

@NullVoxPopuli
Copy link
Collaborator

Param isn't tracked -- this is what i do:
https://github.com/universal-ember/ember-primitives/pull/79/files#diff-5531fdd2c3fe5e2ccff7b580876a323ff6e48bed73881d5e316dd19c184ed689R13

test('with a value', async function (assert) {
    class State {
      @tracked value = 0;
    }

    let state = new State();

    await render(
      <template>
        <Progress @value={{state.value}} as |x|>
          <div id="value">{{x.value}}</div>
          <div id="percent">{{x.percent}}</div>
          <x.Indicator />
        </Progress>
      </template>
    );

You could also use cell, TrackedObject, etc

@NullVoxPopuli
Copy link
Collaborator

NullVoxPopuli commented Aug 14, 2023

Allllso, you can replace

assert.dom(this.element)

With

assert.dom()

this isn't needed in tests, and i kinda want to lint against it

@cah-brian-gantzler
Copy link
Author

One way I see is to create a class with param tracked but that seems overkill for tests from what we had before.

Yes, I had suggested the class above. What should we be teaching as the recommended way? Guides prob needed updated to let people know.

Allllso, you can replace

assert.dom(this.element)

With

assert.dom()

this isn't needed in tests, and i kinda want to lint against it

This was just an example test and was generated from blueprints, so blueprints should be updated.

@NullVoxPopuli
Copy link
Collaborator

What should we be teaching as the recommended way? Guides prob needed updated to let people know.

yeah, the guides are still "Octane", and become less and less relevant as we move towards polaris -- the guides will need a big overhaul before we declare polaris released tho.

so blueprints should be updated.

yes

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

2 participants