-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update flags on context update (#107)
- Loading branch information
Showing
4 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
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
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,59 @@ | ||
import React from 'react'; | ||
import { renderHook } from '@testing-library/react-hooks/native'; | ||
import useFlags from './useFlags'; | ||
import type { IToggle } from 'unleash-proxy-client'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
const toggles = [ | ||
{ | ||
name: 'string', | ||
enabled: true, | ||
variant: { | ||
name: 'string', | ||
enabled: false, | ||
}, | ||
impressionData: false, | ||
}, | ||
{ | ||
name: 'string', | ||
enabled: true, | ||
variant: { | ||
name: 'string', | ||
enabled: false, | ||
}, | ||
impressionData: false, | ||
}, | ||
]; | ||
test('should return flags', () => { | ||
jest.spyOn(React, 'useContext').mockImplementation(() => ({ | ||
client: { | ||
getAllToggles: () => toggles, | ||
on: jest.fn(), | ||
}, | ||
})); | ||
|
||
const { result } = renderHook(() => useFlags()); | ||
expect(result.current).toEqual(toggles); | ||
}); | ||
|
||
test('should update flags on update event', async () => { | ||
const updatedToggles: IToggle[] = []; | ||
const client = { | ||
getAllToggles: jest.fn(), | ||
on: jest.fn(), | ||
off: jest.fn(), | ||
}; | ||
client.getAllToggles.mockReturnValue(toggles); | ||
|
||
jest.spyOn(React, 'useContext').mockImplementation(() => ({ | ||
client, | ||
})); | ||
|
||
const { result } = renderHook(() => useFlags()); | ||
expect(result.current).toEqual(toggles); | ||
client.getAllToggles.mockReturnValue(updatedToggles); | ||
await act(async () => { | ||
client.on.mock.calls[0][1](); | ||
}); | ||
expect(result.current).toEqual(updatedToggles); | ||
}); |
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 |
---|---|---|
@@ -1,10 +1,23 @@ | ||
import { useContext } from 'react'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
import FlagContext from './FlagContext'; | ||
|
||
const useFlags = () => { | ||
const { client } = useContext(FlagContext); | ||
const [flags, setFlags] = useState(client.getAllToggles()); | ||
|
||
return client.getAllToggles(); | ||
useEffect(() => { | ||
const onUpdate = () => { | ||
setFlags(client.getAllToggles()); | ||
}; | ||
|
||
client.on('update', onUpdate); | ||
|
||
return () => { | ||
client.off('update', onUpdate); | ||
}; | ||
}, []); | ||
|
||
return flags; | ||
}; | ||
|
||
export default useFlags; |
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