Skip to content
This repository was archived by the owner on Jul 30, 2018. It is now read-only.

Add ability to mock meta providers#68

Merged
kitsonk merged 2 commits intodojo:masterfrom
kitsonk:feature-meta
Oct 26, 2017
Merged

Add ability to mock meta providers#68
kitsonk merged 2 commits intodojo:masterfrom
kitsonk:feature-meta

Conversation

@kitsonk
Copy link
Member

@kitsonk kitsonk commented Oct 24, 2017

Type: feature

The following has been addressed in the PR:

  • There is a related issue
  • All code matches the style guide
  • Unit or Functional tests are included in the PR

Description:

This PR provides the ability to mock meta providers.

Allows for mocking of meta providers when testing a widget. The method .mockMeta() takes two arguments, first the meta provider class and the second argument being a map of methods to mock on the provider when the harness widget is created.

For example, to provide mocked Dimensions for a widget:

const widget = harness.widget(MyWidget);

const rootDimensions = {
    offset: { height: 100, left: 100, top: 100, width: 100 },
    position: { bottom: 200, left: 100, right: 200, top: 100 },
    scroll: { height: 100, left: 100, top: 100, width: 100 },
    size: { width: 100, height: 100 }
};
const emptyDimensions = {
    offset: { height: 0, left: 0, top: 0, width: 0 },
    position: { bottom: 0, left: 0, right: 0, top: 0 },
    scroll: { height: 0, left: 0, top: 0, width: 0 },
    size: { width: 0, height: 0 }
};

const handle = widget.mockMeta(Dimensions, {
    get(key: string | number) {
        return key === 'root' ? rootDimensions : emptyDimensions;
    }
});

The handle returned from the .mockMeta function can be used to remove the mocks.

To facilitate usage under TypeScript, there is a special context type (MetaMockContext) that is exported from the harness module. This is designed to represent the run-time context of the mock methods, which will allow access to this.invalidate() and this.getNode() which may be required to create an appropriate mock.

For example, to invalidate a widget via the meta provider mock:

import harness, { MetaMockContext } from '@dojo/text-extras/harness';

const widget = harness.widget(MyWidget);

widget.mockMeta(Dimensions, {
    get(this: MetaMockContext<Dimensions>, key: string | number) {
        this.invalidate();
        return {}
    }
})

Resolves #65

@codecov
Copy link

codecov bot commented Oct 24, 2017

Codecov Report

Merging #68 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #68      +/-   ##
==========================================
+ Coverage   99.05%   99.08%   +0.02%     
==========================================
  Files          10       10              
  Lines         637      653      +16     
  Branches      158      160       +2     
==========================================
+ Hits          631      647      +16     
  Partials        6        6
Impacted Files Coverage Δ
src/harness.ts 97.83% <100%> (+0.2%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 369c495...995c8d7. Read the comment docs.

Copy link
Member

@bitpshr bitpshr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the resulting API.

* @param actual The render, just after `afterRender`
*/
actualRender(actual: DNode) {
public actualRender(actual: DNode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have to be explicitly marked as public?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unnecessary, though we have been trying to be more explicit in the visibility operators. I can remove it if it is a concern. I forgot I did it.

Copy link
Contributor

@smhigley smhigley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API looks good. This will make Listbox tests much prettier :)

@kitsonk kitsonk merged commit 2dadb81 into dojo:master Oct 26, 2017
@dylans dylans added this to the 2017.10 milestone Oct 27, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants