-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ensure LocalAccessTokenHandler calls Storage with correct value…
…s and throw errors on get/set
- Loading branch information
1 parent
2343767
commit 91c721b
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/data/usecases/handle-access-token/local-access-token-handler.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {StorageMock} from '@/data/test/mock-storage'; | ||
import {StorageGetError} from '@/domain/errors/storage-get-error'; | ||
import {StorageSetError} from '@/domain/errors/storage-set-error'; | ||
import faker from 'faker'; | ||
import {LocalAccessTokenHandler} from './local-access-token-handler'; | ||
|
||
type SutTypes = { | ||
sut: LocalAccessTokenHandler; | ||
storageMock: StorageMock; | ||
}; | ||
|
||
const makeSut = (): SutTypes => { | ||
const storageMock = new StorageMock(); | ||
const sut = new LocalAccessTokenHandler(storageMock); | ||
return { | ||
sut, | ||
storageMock, | ||
}; | ||
}; | ||
|
||
describe('LocalAccessTokenHandler', () => { | ||
test('Should call Storage with correct values on set', async () => { | ||
const {sut, storageMock} = makeSut(); | ||
const fakeAccessToken = faker.random.uuid(); | ||
await sut.save(fakeAccessToken); | ||
expect(storageMock.key).toBe('accessToken'); | ||
expect(storageMock.value).toBe(fakeAccessToken); | ||
}); | ||
test('Should throw StorageSetError when Storage throws error on set', async () => { | ||
const {sut, storageMock} = makeSut(); | ||
jest.spyOn(storageMock, 'set').mockRejectedValueOnce(new Error()); | ||
const promise = sut.save(faker.random.uuid()); | ||
await expect(promise).rejects.toThrow(new StorageSetError()); | ||
}); | ||
test('Should call Storage with correct values on get', async () => { | ||
const {sut, storageMock} = makeSut(); | ||
const fakeAccessToken = faker.random.uuid(); | ||
jest.spyOn(storageMock, 'get').mockResolvedValueOnce(fakeAccessToken); | ||
const promise = sut.load(); | ||
await expect(promise).resolves.toBe(fakeAccessToken); | ||
}); | ||
test('Should throw StorageGetError when Storage throws error on get', async () => { | ||
const {sut, storageMock} = makeSut(); | ||
jest.spyOn(storageMock, 'get').mockRejectedValueOnce(new Error()); | ||
const promise = sut.load(); | ||
await expect(promise).rejects.toThrow(new StorageGetError()); | ||
}); | ||
test('Should call AsyncStorage to clear storage', () => {}); | ||
test('Should return StorageClearError when failed to clear information with AsyncStorage', () => {}); | ||
}); |