{"size": 3273, "ext": "js", "max_stars_repo_path": "src/store/AuthStore.spec.js", "max_stars_repo_name": "snblickhan/text-editor", "max_stars_repo_stars_event_min_datetime": "2020-10-08T18:03:10.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-02T09:30:43.000Z", "max_issues_repo_path": "src/store/AuthStore.spec.js", "max_issues_repo_name": "snblickhan/text-editor", "max_issues_repo_issues_event_min_datetime": "2019-08-20T14:45:23.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-27T20:39:55.000Z", "max_forks_repo_path": "src/store/AuthStore.spec.js", "max_forks_repo_name": "snblickhan/text-editor", "max_forks_repo_forks_event_min_datetime": "2020-09-29T16:08:14.000Z", "max_forks_repo_forks_event_max_datetime": "2020-09-29T16:08:14.000Z"}
{"max_stars_count": 2, "max_issues_count": 157, "max_forks_count": 1, "avg_line_length": 28.9646017699, "max_line_length": 90, "alphanum_fraction": 0.6361136572}
import auth from 'panoptes-client/lib/auth'
import { AppStore } from './AppStore'
import history from '../history'

const user = { id: '1', display_name: 'A_USER' }

let authStore
const setBearerTokenSpy = jest.fn()

let rootStore = AppStore.create()
Object.defineProperty(
  rootStore.client, 'setBearerToken',
  { writable: true, value: setBearerTokenSpy })

describe('AuthStore', function () {
  it('should exist', function () {
    authStore = rootStore.auth
    expect(authStore).toBeDefined()
  })

  describe('login function success', function () {
    beforeAll(function () {
      jest
        .spyOn(auth, 'checkBearerToken')
        .mockImplementation(() => Promise.resolve())
      jest
        .spyOn(auth, 'signIn')
        .mockImplementation(() => Promise.resolve(user))
      authStore = rootStore.auth
    })

    it('should set the user', async function () {
      let setSubmittingSpy = jest.fn()
      await authStore.login('login', 'password', setSubmittingSpy)
      expect(setSubmittingSpy).toHaveBeenCalledWith(false)
      expect(authStore.user).toBe(user)
      expect(authStore.userName).toBe(user.display_name)
    })
  })

  describe('login function failure', function () {
    const error = 'Failed to find user'

    beforeAll(function () {
      jest
        .spyOn(auth, 'signIn')
        .mockImplementation(() => Promise.reject({ message: error }))
      authStore = rootStore.auth
    })

    it('should set the user', async function () {
      let setSubmittingSpy = jest.fn()
      await authStore.login('login', 'password', setSubmittingSpy)
      expect(setSubmittingSpy).toHaveBeenCalledWith(false)
      expect(authStore.error).toBe(error)
    })
  })

  describe('logout function success', function () {
    let signOutSpy
    const user = { id: '1' }

    beforeAll(function () {
      signOutSpy = jest.spyOn(auth, 'signOut').mockImplementation(() => Promise.resolve())
      rootStore = AppStore.create({ auth: { user } })
      Object.defineProperty(
        rootStore.client, 'setBearerToken',
        { writable: true, value: setBearerTokenSpy })
      authStore = rootStore.auth
    })

    it('should call the sign out function', async function () {
      await authStore.logout()
      expect(authStore.user).toBe(null)
      expect(signOutSpy).toHaveBeenCalled()
    })
  })

  describe('checkCurrent returns user', function () {
    beforeAll(function () {
      jest
        .spyOn(auth, 'checkCurrent')
        .mockImplementation(() => Promise.resolve(user))
      authStore = rootStore.auth
    })

    it('should set the current user', async function() {
      authStore = rootStore.auth
      await authStore.checkCurrent()
      expect(authStore.user).toBe(user)
    })
  })

  describe('checkCurrent returns nothing', function () {
    let pushSpy
    beforeAll(function () {
      pushSpy = jest
        .spyOn(history, 'push')
        .mockImplementation(() => {})
      jest
        .spyOn(auth, 'checkCurrent')
        .mockImplementation(() => Promise.resolve())
      authStore = rootStore.auth
      history.location.pathname = '/projects'
    })

    it('should change the route', async function () {
      await authStore.checkCurrent()
      expect(pushSpy).toHaveBeenCalledWith('/')
    })
  })
})
