Skip to content

Instantly share code, notes, and snippets.

@matthewstokeley
Last active September 29, 2019 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewstokeley/57409c84626c8534468fa965d6a8d04f to your computer and use it in GitHub Desktop.
Save matthewstokeley/57409c84626c8534468fa965d6a8d04f to your computer and use it in GitHub Desktop.
the use of an `expected` parameter to mock data values

The expected pattern for handling unknown data sources from React's ssr rendering library, in this case a data-attribute value, provides an alternative to event propagation and graceful degradation for error handling.

  • is the attribute name safe?
    • if no, return
  • does the node have the attribute?
    • if no, return a value of null unless expected is undefined, in which case, return undefined.
  • assign the value of getAttribute
  • is the value equal to an empty string? return expected
    • otherwise, return the value

This replaces the fault-tolerant pattern of graceful degradation that expects a false value for ambiguous return values with a development environment-friendly data-mocking parameter without the weight of a default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment