-
Notifications
You must be signed in to change notification settings - Fork 1
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
Corrige erro da async storage #42
Conversation
@@ -18,9 +18,20 @@ const ThemeToggle = (): React.JSX.Element => { | |||
return <button onClick={toggleDarkMode}>Trocar de tema</button>; | |||
}; | |||
|
|||
const getPersistedTheme = async (key: string): Promise<string | null> => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Como agora a gestão da persistência é feita por quem usa a lib, eu tiraria esse parâmetro key
e deixaria quem usa responsável por fazer a parametrização. (E o mesmo para a setPersistedTheme
)
themes/DarkModeEnabledContext.tsx
Outdated
const DarkModeEnabledProvider: React.FC<{ | ||
children: ReactNode; | ||
setPersistedTheme: (key: string, value: ThemeType) => Promise<void>; | ||
getPersistedTheme: (key: string) => Promise<string | null>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
O valor devolvido deveria ser ThemeType | null
, não?
themes/DarkModeEnabledContext.tsx
Outdated
if (storedTheme !== null) { | ||
setTheme(storedTheme); | ||
setTheme(storedTheme as ThemeType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fazendo a mudança na tipagem que comentei acima imagino que não precise mais do cast.
Contexto
Ao rodar os testes no SGLearner, me deparei com o seguinte erro — que acontecia somente nos testes mobile e apontava para a lib do async storage — :
![Screenshot 2024-02-29 at 11 11 09](https://private-user-images.githubusercontent.com/29842092/309359791-8500799a-f74b-455b-8ac2-e50226a5c3c5.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0OTAwMzMsIm5iZiI6MTczOTQ4OTczMywicGF0aCI6Ii8yOTg0MjA5Mi8zMDkzNTk3OTEtODUwMDc5OWEtZjc0Yi00NTViLThhYzItZTUwMjI2YTVjM2M1LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEzVDIzMzUzM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTg4MTMwYzI1ZWI0YjMzMDRiM2JkYmVmYjdkMDFmMTU3YzY5N2FmMjRmY2I4MmQ3NGZmYmE0OTk0Y2M2MDc2MGEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.Ns8yV2ISSlcInTgsGZnjB-ULwfNgbWtpXKad49cMDmE)
Tentei ajustar os mocks, atualizar a biblioteca e segui algumas outras soluções apontadas no SO e nas issues da lib, mas não tive sucesso.
Depois de investigar, entendi que o problema é que, resumidamente, a lib async-storage não é feita para ser consumida através de uma biblioteca de terceiros - ou, pelo menos, não da forma como a lib do DS é organizada/distribuída. O async storage precisa, necessariamente, de configurações adicionais para funcionar no mobile.
No SGLearner também temos essa biblioteca instalada e configurada, além do mock do jest, mas não consegui achar uma forma de configurar, no DS, para evitar o erro nos testes. Me parece que o problema acontece pela falta das configurações específicas dos módulos nativos.
Mudanças
Como o problema acontece na lib do DS, decidi ajustar o componente - o DarkModeReadyProvider - que consome o async storage, para recebê-lo como prop. Dessa forma, conseguimos resolver o erro, já que o async storage
Acredito que a DX não fique ruim, já que esse componente é chamado somente nos arquivos
app.tsx
- e seremos nós que adicionaremos esse controle de estado.Como testar
Buildar a lid do ds e mover para o SGLearner;
Rodar o teste
yarn test rn/js/components/boxes/__tests__/SectionTitleBox.test.tsx
(era um dos que quebrava)