This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add required APIs for react streaming #177
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ef931c
feat: add required APIs for react streaming
Matthew-Mallimo 825e153
feat: enable module context to store more than just the name
Matthew-Mallimo a395846
feat(holocron): new streaming API
Matthew-Mallimo feccb30
fix: initiate module context with useMemo
Matthew-Mallimo 82a8381
test: reformat eslint disables
Matthew-Mallimo 30e41f6
refactor: move context holocronModule
Matthew-Mallimo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,99 @@ | ||
import React from 'react'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies -- monorepo, this is at the root | ||
import { renderHook } from '@testing-library/react'; | ||
import { useAsyncModuleData, ReactStreamingContext } from '../src/reactStreaming'; | ||
import { ModuleContext } from '../src/holocronModule'; | ||
|
||
describe('reactStreaming', () => { | ||
it('exports ReactStreamingContext', () => { | ||
expect(ReactStreamingContext).toBeDefined(); | ||
}); | ||
|
||
describe('useAsyncModuleData', () => { | ||
/* eslint-disable | ||
react/display-name, | ||
react/prop-types, | ||
react/jsx-no-constructed-context-values -- test component */ | ||
const Providers = ({ moduleName, promise, key }) => ({ children }) => ( | ||
<ReactStreamingContext.Provider value={{ [moduleName]: { [key]: promise } }}> | ||
<ModuleContext.Provider value={{ moduleName }}> | ||
{children} | ||
</ModuleContext.Provider> | ||
</ReactStreamingContext.Provider> | ||
/* eslint-enable | ||
react/display-name, | ||
react/prop-types, | ||
react/jsx-no-constructed-context-values -- test component */ | ||
); | ||
it('should throw a promise if the data is not yet resolved', () => { | ||
const key = 'test'; | ||
const moduleName = 'testModule'; | ||
const streamedPromise = new Promise(() => {}); | ||
const { result } = renderHook(() => { | ||
try { | ||
return useAsyncModuleData(key); | ||
} catch (promise) { | ||
return promise; | ||
} | ||
}, { | ||
wrapper: Providers({ moduleName, promise: streamedPromise, key }), | ||
}); | ||
expect(result.current).toBe(streamedPromise); | ||
}); | ||
|
||
it('should return data once the promise is resolved', () => { | ||
const key = 'test'; | ||
const moduleName = 'testModule'; | ||
let resolve; | ||
const streamedPromise = new Promise((res) => { resolve = res; }); | ||
const { result, rerender } = renderHook(() => { | ||
try { | ||
return useAsyncModuleData(key); | ||
} catch (promise) { | ||
return promise; | ||
} | ||
}, { | ||
wrapper: Providers({ moduleName, promise: streamedPromise, key }), | ||
}); | ||
resolve(); | ||
streamedPromise.data = 'testData'; | ||
rerender(); | ||
expect(result.current).toBe('testData'); | ||
}); | ||
|
||
it('should throw an error if the promise is rejected', () => { | ||
const key = 'test'; | ||
const moduleName = 'testModule'; | ||
let reject; | ||
const streamedPromise = new Promise((_, rej) => { reject = rej; }); | ||
const { result, rerender } = renderHook(() => { | ||
try { | ||
return useAsyncModuleData(key); | ||
} catch (error) { | ||
return error; | ||
} | ||
}, { | ||
wrapper: Providers({ moduleName, promise: streamedPromise, key }), | ||
}); | ||
reject(); | ||
streamedPromise.error = 'testError'; | ||
rerender(); | ||
expect(result.current).toBe('testError'); | ||
}); | ||
it('should return undefined if there is no promise', () => { | ||
const key = 'test'; | ||
const moduleName = 'testModule'; | ||
const streamedPromise = undefined; | ||
const { result } = renderHook(() => { | ||
try { | ||
return useAsyncModuleData(key); | ||
} catch (promise) { | ||
return promise; | ||
} | ||
}, { | ||
wrapper: Providers({ moduleName, promise: streamedPromise, key }), | ||
}); | ||
expect(result.current).toBe(undefined); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Wouldn't it make more sense to include it in the devDependencies for this workspace than to disable the rule?