How to use setupResponses method in storybook-root

Best JavaScript code snippet using storybook-root

index.spec.ts

Source:index.spec.ts Github

copy

Full Screen

...27 configureResponse()28}29test('axiosTrials(axios, { retries, retryCondition })', t => {30 const client = axios.create();31 setupResponses(client, [32 () =>33 nock('http://example.com')34 .get('/test')35 .reply(200, 'It worked!')36 ])37 axiosTrials(client, { retries: 0 })38 client.get('http://example.com/test').then(result => {39 t.same(200, result.status)40 nock.cleanAll()41 nock.enableNetConnect()42 t.end()43 })44})45test('When the response is an error', t => {46 const client = axios.create();47 setupResponses(client, [48 () =>49 nock('http://example.com')50 .get('/test')51 .replyWithError(NETWORK_ERROR),52 () =>53 nock('http://example.com')54 .get('/test')55 .reply(200, 'It worked!')56 ])57 const retryCondition = (error: any) => {58 t.same(error, NETWORK_ERROR)59 nock.cleanAll()60 nock.enableNetConnect()61 t.end()62 return false;63 }64 axiosTrials(client, { retries: 1, retryCondition })65 client.get('http://example.com/test').catch(() => { })66})67test('When it satisfies the retry condition', t => {68 const client = axios.create()69 setupResponses(client, [70 () =>71 nock('http://example.com')72 .get('/test')73 .replyWithError(NETWORK_ERROR),74 () =>75 nock('http://example.com')76 .get('/test')77 .reply(200, 'It worked!')78 ])79 axiosTrials(client, { retries: 1, retryCondition: () => true })80 client.get('http://example.com/test').then(result => {81 t.same(result.status, 200)82 nock.cleanAll()83 nock.enableNetConnect()84 t.end()85 })86})87test('should not run transformRequest twice', t => {88 const client = axios.create({89 transformRequest: [JSON.stringify]90 })91 setupResponses(client, [92 () =>93 nock('http://example.com')94 .post('/test', body => {95 t.same(body.a, 'b')96 return true;97 })98 .replyWithError(NETWORK_ERROR),99 () =>100 nock('http://example.com')101 .post('/test', body => {102 t.same(body.a, 'b')103 return true;104 })105 .reply(200, 'It worked!')106 ])107 axiosTrials(client, { retries: 3, retryCondition: () => true })108 client.post('http://example.com/test', { a: 'b' }).then(result => {109 t.same(result.status, 200)110 nock.cleanAll()111 nock.enableNetConnect()112 t.end()113 })114})115test('should reject with a request error if retries <= 0', t => {116 const client = axios.create()117 setupResponses(client, [118 () =>119 nock('http://example.com')120 .get('/test')121 .replyWithError(NETWORK_ERROR)122 ])123 axiosTrials(client, { retries: 0, retryCondition: () => true })124 client.get('http://example.com/test').catch(error => {125 t.same(error, NETWORK_ERROR)126 nock.cleanAll()127 nock.enableNetConnect()128 t.end()129 })130})131test('should reject with a request error if there are more errors than retries', t => {132 const client = axios.create()133 setupResponses(client, [134 () =>135 nock('http://example.com')136 .get('/test')137 .replyWithError(new Error('foo error')),138 () =>139 nock('http://example.com')140 .get('/test')141 .replyWithError(NETWORK_ERROR)142 ])143 axiosTrials(client, { retries: 1, retryCondition: () => true })144 client.get('http://example.com/test').catch(error => {145 t.same(error, NETWORK_ERROR)146 nock.cleanAll()147 nock.enableNetConnect()148 t.end()149 });150})151test('should honor the original `timeout` across retries', t => {152 const client = axios.create()153 setupResponses(client, [154 () =>155 nock('http://example.com')156 .get('/test')157 .delay(75)158 .replyWithError(NETWORK_ERROR),159 () =>160 nock('http://example.com')161 .get('/test')162 .delay(75)163 .replyWithError(NETWORK_ERROR),164 () =>165 nock('http://example.com')166 .get('/test')167 .reply(200)168 ])169 axiosTrials(client, { retries: 3 })170 client.get('http://example.com/test', { timeout: 100 })171 .catch(error => {172 t.same(error.code, 'ECONNABORTED')173 nock.cleanAll()174 nock.enableNetConnect()175 t.end()176 })177})178test('should reset the original `timeout` between requests', t => {179 const client = axios.create()180 setupResponses(client, [181 () =>182 nock('http://example.com')183 .get('/test')184 .delay(75)185 .replyWithError(NETWORK_ERROR),186 () =>187 nock('http://example.com')188 .get('/test')189 .delay(75)190 .replyWithError(NETWORK_ERROR),191 () =>192 nock('http://example.com')193 .get('/test')194 .reply(200)195 ])196 axiosTrials(client, { retries: 3, shouldResetTimeout: true })197 client.get('http://example.com/test', { timeout: 100 }).then(result => {198 t.same(result.status, 200)199 nock.cleanAll()200 nock.enableNetConnect()201 t.end()202 })203})204test('should reject with errors without a `config` property without retrying', t => {205 const client = axios.create()206 setupResponses(client, [207 () =>208 nock('http://example.com')209 .get('/test')210 .replyWithError(NETWORK_ERROR),211 () =>212 nock('http://example.com')213 .get('/test')214 .reply(200)215 ])216 // Force returning a plain error without extended information from Axios217 const generatedError = new Error()218 client.interceptors.response.use(undefined, () => Promise.reject(generatedError))219 axiosTrials(client, { retries: 1, retryCondition: () => true })220 client.get('http://example.com/test').catch(error => {221 t.same(error, generatedError)222 nock.cleanAll()223 nock.enableNetConnect()224 t.end()225 })226})227test('when it does NOT satisfy the retry condition', t => {228 const client = axios.create()229 setupResponses(client, [230 () =>231 nock('http://example.com')232 .get('/test')233 .replyWithError(NETWORK_ERROR),234 () =>235 nock('http://example.com')236 .get('/test')237 .reply(200, 'It worked!')238 ]);239 axiosTrials(client, { retries: 1, retryCondition: () => false })240 client.get('http://example.com/test').catch(error => {241 t.same(error, NETWORK_ERROR)242 nock.cleanAll()243 nock.enableNetConnect()244 t.end()245 })246})247test('With custom retry it should execute for each retry', t => {248 const client = axios.create();249 setupResponses(client, [250 () =>251 nock('http://example.com')252 .get('/test')253 .replyWithError(NETWORK_ERROR),254 () =>255 nock('http://example.com')256 .get('/test')257 .replyWithError(NETWORK_ERROR),258 () =>259 nock('http://example.com')260 .get('/test')261 .replyWithError(NETWORK_ERROR),262 () =>263 nock('http://example.com')...

Full Screen

Full Screen

axiosRetry.spec.ts

Source:axiosRetry.spec.ts Github

copy

Full Screen

...35 nock.enableNetConnect()36 })37 it('【正常系】200が返されるのでリトライされず、retryCountの値は変わらない', done => {38 const client = axios.create()39 setupResponses(client, [40 // 初回リクエスト41 () =>42 nock('http://example.com')43 .get('/test')44 .reply(200)45 ])46 axiosRetry(client)47 client48 .get('http://example.com/test')49 .then(result => {50 expect(result.status).toBe(200)51 expect(result.config[namespace]?.retryCount).toBe(0)52 done()53 })54 })55 it('【異常系】エラーが返されるのでリトライされ、retryCountの値が増える', done => {56 const client = axios.create()57 setupResponses(client, [58 // 初回リクエスト59 () =>60 nock('http://example.com')61 .get('/test')62 .replyWithError(NETWORK_ERROR),63 // リトライ 1回目64 () =>65 nock('http://example.com')66 .get('/test')67 .replyWithError(NETWORK_ERROR),68 // リトライ 2回目69 () =>70 nock('http://example.com')71 .get('/test')72 .reply(200)73 ])74 // client.interceptors.response.use(undefined, (error) => {75 // console.log(error.code) // ECONNRESET76 // return Promise.reject(error)77 // })78 axiosRetry(client, { retries: 3 })79 client80 .get('http://example.com/test')81 .then(result => {82 expect(result.status).toBe(200)83 expect(result.config[namespace]?.retryCount).toBe(2)84 done()85 })86 })87})88/**89 * 正常系90 * - タイムアウトしない リトライしない91 * 異常系92 * - タイムアウトしない リトライする93 * - タイムアウトする リトライしない94 * - タイムアウトする リトライする95 */96describe('timeout', () => {97 afterEach(() => {98 nock.cleanAll()99 nock.enableNetConnect()100 })101 it('【正常系】タイムアウト以内にレスポンスが返されるのでリトライされず、timeoutの値は変わらない', done => {102 const client = axios.create({103 timeout: 500,104 })105 setupResponses(client, [106 () =>107 nock('http://example.com')108 .get('/test')109 .delay(400) // timeout=500110 .reply(200)111 ])112 axiosRetry(client)113 client114 .get('http://example.com/test')115 .then(result => {116 expect(result.status).toBe(200)117 expect(result.config.timeout).toBe(500)118 done()119 })120 })121 it('【異常系】タイムアウト以内にレスポンスが返されるがエラーなのでリトライされ、timeoutの値が増える', done => {122 const client = axios.create({123 timeout: 500,124 })125 setupResponses(client, [126 () =>127 nock('http://example.com')128 .get('/test')129 .delay(400) // timeout=500130 .replyWithError(NETWORK_ERROR),131 () =>132 nock('http://example.com')133 .get('/test')134 .delay(800) // timeout=1000135 .replyWithError(NETWORK_ERROR),136 () =>137 nock('http://example.com')138 .get('/test')139 .delay(1200) // timeout=1500140 .reply(200)141 ])142 axiosRetry(client, { retries: 3, retryDelay: () => 500 })143 client144 .get('http://example.com/test')145 .then(result => {146 expect(result.status).toBe(200)147 /**148 * 1. timeout=500 + delay=500 = 1000149 * 2. timeout=1000 + delay=500 = 1500150 * 3. timeoutの値は変化しない (200が返されるのでaxiosRetryのonRejectedは実行されない)151 */152 expect(result.config.timeout).toBe(1500)153 done()154 })155 })156 it('【異常系】タイムアウト以内にレスポンスが返されないがリトライされず、timeoutの値は変わらない', done => {157 const client = axios.create({158 timeout: 100,159 })160 // タイムアウトした場合 code=ECONNABORTED161 setupResponses(client, [162 () =>163 nock('http://example.com')164 .get('/test')165 .delay(500) // timeout=100166 .replyWithError(NETWORK_ERROR),167 ])168 axiosRetry(client)169 client170 .get('http://example.com/test')171 .catch(error => {172 expect(error.config.timeout).toBe(100)173 done()174 })175 })176 it('【異常系】タイムアウト以内にレスポンスが返されないのでリトライされ、timeoutの値が増える', done => {177 const client = axios.create({178 timeout: 100,179 })180 // タイムアウトした場合 code=ECONNABORTED181 setupResponses(client, [182 () =>183 nock('http://example.com')184 .get('/test')185 .delay(500) // timeout=100186 .replyWithError(NETWORK_ERROR),187 () =>188 nock('http://example.com')189 .get('/test')190 .delay(500) // timeout=200191 .replyWithError(NETWORK_ERROR),192 () =>193 nock('http://example.com')194 .get('/test')195 .delay(500) // timeout=300...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

...39 };40 [200, 503, 404].forEach(statusCode => {41 describe('when endpoint returns ' + statusCode, () => {42 before(() => {43 return setupResponses([statusCode]);44 });45 it('does not retry request', () => {46 return fetchRetry(baseUrl)47 .then(getCallCount)48 .should.eventually.equal(1);49 });50 });51 });52 describe('when configured to retry on a specific HTTP code', () => {53 describe('and it never succeeds', () => {54 const retryOn = [503];55 beforeEach(() => {56 return setupResponses([503, 503, 503, 503]);57 });58 it('retries the request #retries times', () => {59 const init = {60 retries: 3,61 retryDelay: 100,62 retryOn63 };64 const expectedCallCount = init.retries + 1;65 return fetchRetry(baseUrl, init)66 .then(getCallCount)67 .should.eventually.equal(expectedCallCount);68 });69 it('eventually resolves the promise with the response of the last request', () => {70 const init = {71 retries: 3,72 retryDelay: 100,73 retryOn74 };75 const expectedResponse = {76 status: 503,77 ok: false78 };79 return fetchRetry(baseUrl, init)80 .then(response => {81 return {82 status: response.status,83 ok: response.ok84 };85 })86 .should.become(expectedResponse);87 });88 });89 describe('and it eventually succeeds', () => {90 const retryOnStatus = 503;91 const responses = [503, 503, 200];92 const requestsToRetry = responses93 .filter(response => response === retryOnStatus)94 .length;95 beforeEach(() => {96 return setupResponses(responses);97 });98 it('retries the request up to #retries times', () => {99 const init = {100 retries: 3,101 retryDelay: 100,102 retryOn: [retryOnStatus]103 };104 const expectedCallCount = requestsToRetry + 1;105 return fetchRetry(baseUrl, init)106 .then(getCallCount)107 .should.eventually.equal(expectedCallCount);108 });109 it('eventually resolves the promise with the received response of the last request', () => {110 const init = {111 retries: 3,112 retryDelay: 100,113 retryOn: [retryOnStatus]114 };115 const expectedResponse = {116 status: 200,117 ok: true118 };119 return fetchRetry(baseUrl, init)120 .then(response => {121 return {122 status: response.status,123 ok: response.ok124 };125 })126 .should.become(expectedResponse);127 });128 });129 });130 describe('when configured to retry on a set of HTTP codes', () => {131 describe('and it never succeeds', () => {132 const retryOn = [503, 404];133 beforeEach(() => {134 return setupResponses([503, 404, 404, 503]);135 });136 it('retries the request #retries times', () => {137 const init = {138 retries: 3,139 retryDelay: 100,140 retryOn141 };142 const expectedCallCount = init.retries + 1;143 return fetchRetry(baseUrl, init)144 .then(getCallCount)145 .should.eventually.equal(expectedCallCount);146 });147 });148 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import setupResponses from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import { withInfo } from '@storybook/addon-info';4import { withKnobs } from '@storybook/addon-knobs';5import { withA11y } from '@storybook/addon-a11y';6import { withTests } from '@storybook/addon-jest';7import { withPerformance } from 'storybook-addon-performance';8import { withScreenshot } from 'storybook-chrome-screenshot';9import { withResponsive } from 'storybook-addon-responsive';10import { withConsole } from '@storybook/addon-console';11import { withViewport } from '@storybook/addon-viewport';12import { withPuppeteer } from 'storybook-addon-puppeteer';13import { withCssResources } from '@storybook/addon-cssresources';14import { withContexts } from '@storybook/addon-contexts/react';15import { withRedux } from 'storybook-addon-redux';16import { withOptions } from '@storybook/addon-options';17setupResponses();18const stories = storiesOf('test', module);19stories.addDecorator(withKnobs);20stories.addDecorator(withInfo);21stories.addDecorator(withA11y);22stories.addDecorator(23 withTests({24 results: {25 summary: {26 },27 {28 },29 {30 },31 },32 })33);34stories.addDecorator(withPerformance);35stories.addDecorator(withScreenshot);36stories.addDecorator(withResponsive);37stories.addDecorator((storyFn, context) => withConsole()(storyFn)(context));38stories.addDecorator(withViewport);39stories.addDecorator(withPuppeteer);40stories.addDecorator(withCssResources);41stories.addDecorator(42 withContexts([43 {44 {45 props: {46 { name: 'light', value: 'light' },47 { name: 'dark', value: 'dark' },48 },

Full Screen

Using AI Code Generation

copy

Full Screen

1import { setupResponses } from "storybook-root-decorator";2setupResponses();3setupResponses({});4setupResponses({5 defaultResponse: {6 data: {7 },8 },9});10setupResponses({11 defaultResponse: {12 data: {13 },14 },15 mockResponse: {16 data: {17 },18 },19});20setupResponses({21 defaultResponse: {22 data: {23 },24 },25 mockResponse: {26 data: {27 },28 },29});30setupResponses({31 defaultResponse: {32 data: {33 },34 },35 mockResponse: {36 data: {37 },38 },39});40setupResponses({41 defaultResponse: {42 data: {43 },44 },45 mockResponse: {46 data: {47 },48 },49});50setupResponses({51 defaultResponse: {52 data: {

Full Screen

Using AI Code Generation

copy

Full Screen

1import { setupResponses } from 'storybook-root-decorator';2import { setupResponses } from 'storybook-root-decorator';3import { setupResponses } from 'storybook-root-decorator';4import { setupResponses } from 'storybook-root-decorator';5import { setupResponses } from 'storybook-root-decorator';6import { setupResponses } from 'storybook-root-decorator';7import { setupResponses } from 'storybook-root-decorator';8import { setupResponses } from 'storybook-root-decorator';9import { setupResponses } from 'storybook-root-decorator';10import { setupResponses } from 'storybook-root-decorator';11import { setupResponses } from 'storybook-root-decorator';12import { setupResponses } from 'storybook-root-decorator';13import { setupResponses } from 'storybook-root-decorator';14import { setupResponses } from 'storybook-root-decorator';15import { setupResponses } from 'storybook-root-decorator';16import { setupResponses } from 'storybook-root-decorator';

Full Screen

Using AI Code Generation

copy

Full Screen

1import { setupResponses } from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import { withKnobs } from '@storybook/addon-knobs';4import MyComponent from './MyComponent';5import data from './data';6storiesOf('MyComponent', module)7 .addDecorator(withKnobs)8 .add('default', () => {9 setupResponses(data);10 return <MyComponent />;11 });12{13 "path1": {14 "method1": {15 "response1": {16 "headers": {},17 "body": {}18 },19 "response2": {20 "headers": {},21 "body": {}22 }23 },24 "method2": {25 "response1": {26 "headers": {},27 "body": {}28 },29 "response2": {30 "headers": {},31 "body": {}32 }33 }34 },35 "path2": {36 "method1": {37 "response1": {38 "headers": {},39 "body": {}40 },41 "response2": {42 "headers": {},43 "body": {}44 }45 },46 "method2": {47 "response1": {48 "headers": {},49 "body": {}50 },51 "response2": {52 "headers": {},53 "body": {}54 }55 }56 }57}58import { createData } from 'storybook-root-decorator';59import { path1, path2

Full Screen

Using AI Code Generation

copy

Full Screen

1import { setupResponses } from 'storybook-root-decorator';2import { setupResponses } from 'storybook-root-decorator';3import { setupResponses } from 'storybook-root-decorator';4import { setupResponses } from 'storybook-root-decorator';5import { setupResponses } from 'storybook-root-decorator';6import { setupResponses } from 'storybook-root-decorator';7import { setupResponses } from 'storybook-root-decorator';8import { setupResponses } from 'storybook-root-decorator';9import { setupResponses } from 'storybook-root-decorator';10import { setupResponses } from 'storybook-root-decorator';11import { setupResponses } from 'storybook-root-decorator';12import { setupResponses } from 'storybook-root-decorator';13import { setupResponses } from 'storybook-root-decorator';

Full Screen

Using AI Code Generation

copy

Full Screen

1import { setupResponses } from 'storybook-root-decorator';2setupResponses();3import { storiesOf } from '@storybook/react';4import { withKnobs } from '@storybook/addon-knobs';5import { withMockedResponses } from 'storybook-root-decorator';6import MyComponent from '../src/MyComponent';7storiesOf('MyComponent', module)8 .addDecorator(withKnobs)9 .addDecorator(withMockedResponses('/path/to/responses.json'))10 .add('default', () => <MyComponent />);11{12 {13 "body": {14 }15 }16}17import { storiesOf } from '@storybook/react';18import { withKnobs } from '@storybook/addon-knobs';19import { withMockedResponses } from 'storybook-root-decorator';20import MyComponent from '../src/MyComponent';21storiesOf('MyComponent', module)22 .addDecorator(withKnobs)23 .addDecorator(withMockedResponses('/path/to/responses.json'))24 .add('default', () => <MyComponent />)25 .add('with different response', () => <MyComponent />, {26 });27{28 {29 "body": {30 }31 }32}33{34 {35 "body": {

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-root automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful