Skip to content

Commit

Permalink
Feature: Support React Native environments (#115)
Browse files Browse the repository at this point in the history
* fix: support react-native environments

* remove console logs

---------

Co-authored-by: Yann Braga <[email protected]>
  • Loading branch information
SethDavenport and yannbf authored Oct 26, 2023
1 parent 4d3e7a9 commit d283201
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 0 additions & 2 deletions packages/docs/src/demos/fetch/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ function useFetchFilms() {
})
.then((res) => res.json())
.then((data) => {
console.log({ data })
setStatus('success');
setData(data.results);
})
Expand All @@ -36,7 +35,6 @@ function useFetchFilms() {
export function App() {
const { status, data: films } = useFetchFilms();

console.log({ status, films })
if (status === 'loading') {
return <p>Fetching Star Wars data...</p>;
}
Expand Down
1 change: 0 additions & 1 deletion packages/docs/src/demos/fetch/App.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const MockedSuccess = {
msw: {
handlers: [
rest.get('https://swapi.dev/api/films/', (req, res, ctx) => {
console.log('MATCHED!')
return res(
ctx.json({
results: films,
Expand Down
8 changes: 7 additions & 1 deletion packages/msw-addon/src/mswDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ type Context = {
}

const IS_BROWSER = !isNodeProcess()
const IS_REACT_NATIVE = typeof navigator !== 'undefined' && navigator.product === 'ReactNative'
let api: SetupApi
let workerPromise: Promise<unknown>

export function initialize(options?: InitializeOptions, initialHandlers: RequestHandler[] = []): SetupApi {
if (IS_BROWSER) {
if (IS_REACT_NATIVE) {
const { setupServer } = require('msw/native')
const server = setupServer(...initialHandlers)
workerPromise = server.listen(options)
api = server;
} else if (IS_BROWSER) {
const { setupWorker } = require('msw')
const worker = setupWorker(...initialHandlers)
workerPromise = worker.start(options)
Expand Down

1 comment on commit d283201

@vercel
Copy link

@vercel vercel bot commented on d283201 Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

msw-storybook-addon – ./

msw-sb.vercel.app
msw-storybook-addon-git-main-mswjs.vercel.app
msw-storybook-addon-mswjs.vercel.app

Please sign in to comment.