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

Declaratively define order that responses are delivered #515

Open
wheresrhys opened this issue Mar 14, 2020 · 1 comment
Open

Declaratively define order that responses are delivered #515

wheresrhys opened this issue Mar 14, 2020 · 1 comment

Comments

@wheresrhys
Copy link
Owner

Might be nice to declaratively define race conditions

e.g.

fm
  .once('blah', 200, {order: 1})
  .once('bloop', 200, {order: 2})

// code to test.. not a very exciting example
let a = 0;
fetch('bloop').then(() => a = 2)
fetch('blah').then(() => a = 1)

expect(a).toEqual(1) // true

Needs some thought as should probably only be available to 'oncey' methods. Also, maybe order is not the right abstraction. Maybe something a bit more like defining a dependency on another call is called for:

fm
  .once({name: 'blah', ...}, 200,)
  .once({name: 'bloop', ...} 200, {waitFor: 'blah'})

// code to test.. not a very exciting example
let a = 0;
fetch('bloop').then(() => a = 2)
fetch('blah').then(() => a = 1)

expect(a).toEqual(1) // true
@chet-manley
Copy link
Contributor

I think your second suggestion is much more useful and declarative. As a more fleshed-out arbitrary example, let's say you have a scenario where a call to .../api/v2/users requires a previous call to .../api/v2/auth:

fm
  .once({
    name: 'auth-route',
    url: 'path:/api/v2/auth',
    response: new Response(...)
  })
  .once({
    name: 'users-route',
    url: 'path:/api/v2/users',
    response: new Response(...),
    requires: ['auth-route']
  })

// if not already authorized, app should fetch auth token automatically
await expect(app.get('users')).resolves.toEqual(...)

// or, app should fetch auth token automatically, but throws on bad credentials
await expect(app.get('users')).rejects.toThrow(APIAuthError)

// or, app should fetch auth token automatically, but rejects on bad credentials
await expect(app.get('users')).rejects.toBeInstanceOf(APIAuthError)

// no matter the app's implementation, the mocked fetch still works

consider:

  • requires should allow a string or Array of strings, containing named route(s)
  • A call to a route with a requires property
    • should throw if all required routes have not completed
    • should throw if any required routes do not exist
    • should otherwise pass requires check
  • Undefined required routes
    • should throw in route call instead of declaration
      • allows required route(s) to be defined out of order

It seems prudent to only allow this functionality on named routes, to eliminate considerable implementation overhead, and thus complexity. This implementation also allows nested required routes without any extra work: users requires auth requires Oauth, et al.

@wheresrhys wheresrhys changed the title Order Declaratively define order that responses are delivered Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants