0
votes

I can't seem to figure out why a very simple integration test is not passing

Given an ember data model setting.js

export default class SettingModel extends Model {
  @attr('string') name;
  @attr('number') value;
}

I am rendering this model with a component

<MyComponent @setting={{setting}}
             @name={{setting.name}}
             @value={{setting.value}}
             @values={{array 0 1 2}}
             @saveSetting={{saveSetting}} />

The component renders all the possible values (@values) and applies an active class on the one which equals the current value

{{#each @values as |value|}}
  <div class="btn {{if (eq @value value) "active"}}" {{on "click" (fn @saveSetting @setting value)}}>
    {{value}}
  </div>
{{/each}}

I wanted to write a simple test where clicking another button updates the active class, but the last assertion always fails. The active class is never updated. In this example, I have used ember-cli mirage

  test('updates new saved value', async function(assert) {
    this.server.get('/settings/:id', () => ({ settings: { id: 1, name: "settingName", value: 1 }}), 200);
    let setting = await this.owner.lookup('service:store').findRecord('setting', 1);
    this.set('setting', setting);
    this.set('name', setting.get('name'));
    this.set('value', setting.get('value'));
    this.set('values', [0, 1, 2]);
    this.set('saveSettingFn', (setting, newValue) => {
      setting.set('value', newValue);
      setting.save().then(() => console.log('also saved!'));
      console.log('saved!');
    });

    await render(hbs`<MyComponent @setting={{this.setting}}
                                  @name={{this.name}}
                                  @value={{this.value}}
                                  @values={{this.values}}
                                  @saveSetting={{this.saveSettingFn}} />`);

    // middle option active
    assert.ok(this.element.querySelectorAll('.btn')[1].getAttribute('class').includes('active'), 'second button active');
    // set up save handler
    this.server.put('/settings/:id', () => ({ settings: { id: 1, name: "settingName", value: 1 }}), 200);
    // click the first button
    await click('.btn');
    // first option active
    assert.ok(this.element.querySelectorAll('.btn')[0].getAttribute('class').includes('active'), 'first button active');

I've created an example project here with a failing test https://github.com/spicalous/component-test-example/blob/master/tests/integration/components/my-component-test.js#L55

  1. It looks like the value on the model has been saved
  2. I have tried using waitFor test helper
  3. I have tried await settled();

Any help is appreciated! Thank you!

1

1 Answers

0
votes

your saveSettingFn function does nothing. So nothing happens.