fourTheorem / mock-amplify-auth

MIT License
17 stars 4 forks source link

Are there any available instructions for use? #1

Open brandondurham opened 4 years ago

brandondurham commented 4 years ago

I’m not entirely sure how to use these. Is there any documentation you can share?

eoinsha commented 4 years ago

The library is quite simple and is meant to be a drop-in replacement for Amplify Auth as you would use it for Cognito User Pool authorization. It allows you to use your front end running with a server-less-offline back end.

An example of it being used is here: https://github.com/fourTheorem/slic-starter/blob/master/frontend/src/auth-provider.js Associated redux action creators are here: https://github.com/fourTheorem/slic-starter/blob/master/frontend/src/actions/auth.js

Hope this helps.

melaku-z commented 4 years ago

I get: "Auth.currentAuthenticatedUser is not a function" error. My setup is just: import Auth from 'mock-amplify-auth'

I see Auth.currentSession() used in the example above. Does this mean currentAuthenticatedUser() is not supported, or am I using it wrong?

mayteio commented 4 years ago

Correct me if I am wrong @eoinsha, however, if using jest you'd do something like this;

import mockAuth from 'mock-amplify-auth'
jest.mock('@aws-amplify/auth', mockAuth);

test('should', () => {
  // ...
})

If you want to extend it for currentAuthenticatedUser (we need to mock completeNewPassword), do so before running jest.mock:

import mockAuth from 'mock-amplify-auth';

// build your mocks - you could use `mockAuth` if you need access to internal state.
// personally, I only need jest.fn() 
const mockMissingAuthFunctions = {
  completeNewPassword: jest.fn(),
  currentAuthenticatedUser: jest.fn()
}

jest.mock('@aws-amplify/auth', () => ({
  ...mockAuth,
  ...mockMissingAuthFunctions
}))

test('should', () => {
  // ...
})
mattpage commented 4 years ago

Here's how I did it using Jest

import mockAuth from 'mock-amplify-auth';

jest.mock('aws-amplify', () => ({
  Auth: {
    ...mockAuth,
  }
}));
ferrero1987 commented 2 years ago

does exist @types/mock-amplify-auth?