Best JavaScript code snippet using playwright-internal
codegen.spec.js
Source:codegen.spec.js
...14var src_1 = require("../src");15var runtimeHelpers_1 = require("../src/runtimeHelpers");16var testUtils_1 = require("./testUtils");17var shared_1 = require("@vue/shared");18function createRoot(options) {19 if (options === void 0) { options = {}; }20 return __assign({ type: 0, children: [], helpers: [], components: [], directives: [], hoists: [], cached: 0, codegenNode: src_1.createSimpleExpression("null", false), loc: src_1.locStub }, options);21}22describe('compiler: codegen', function () {23 test('module mode preamble', function () {24 var root = createRoot({25 helpers: [runtimeHelpers_1.CREATE_VNODE, runtimeHelpers_1.RESOLVE_DIRECTIVE]26 });27 var code = src_1.generate(root, { mode: 'module' }).code;28 expect(code).toMatch("import { " + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.CREATE_VNODE] + ", " + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.RESOLVE_DIRECTIVE] + " } from \"vue\"");29 expect(code).toMatchSnapshot();30 });31 test('function mode preamble', function () {32 var root = createRoot({33 helpers: [runtimeHelpers_1.CREATE_VNODE, runtimeHelpers_1.RESOLVE_DIRECTIVE]34 });35 var code = src_1.generate(root, { mode: 'function' }).code;36 expect(code).toMatch("const _Vue = Vue");37 expect(code).toMatch("const { " + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.CREATE_VNODE] + ": _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.CREATE_VNODE] + ", " + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.RESOLVE_DIRECTIVE] + ": _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.RESOLVE_DIRECTIVE] + " } = _Vue");38 expect(code).toMatchSnapshot();39 });40 test('function mode preamble w/ prefixIdentifiers: true', function () {41 var root = createRoot({42 helpers: [runtimeHelpers_1.CREATE_VNODE, runtimeHelpers_1.RESOLVE_DIRECTIVE]43 });44 var code = src_1.generate(root, {45 mode: 'function',46 prefixIdentifiers: true47 }).code;48 expect(code).not.toMatch("const _Vue = Vue");49 expect(code).toMatch("const { " + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.CREATE_VNODE] + ", " + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.RESOLVE_DIRECTIVE] + " } = Vue");50 expect(code).toMatchSnapshot();51 });52 test('assets', function () {53 var root = createRoot({54 components: ["Foo", "bar-baz", "barbaz"],55 directives: ["my_dir"]56 });57 var code = src_1.generate(root, { mode: 'function' }).code;58 expect(code).toMatch("const _component_Foo = _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.RESOLVE_COMPONENT] + "(\"Foo\")\n");59 expect(code).toMatch("const _component_bar_baz = _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.RESOLVE_COMPONENT] + "(\"bar-baz\")\n");60 expect(code).toMatch("const _component_barbaz = _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.RESOLVE_COMPONENT] + "(\"barbaz\")\n");61 expect(code).toMatch("const _directive_my_dir = _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.RESOLVE_DIRECTIVE] + "(\"my_dir\")\n");62 expect(code).toMatchSnapshot();63 });64 test('hoists', function () {65 var root = createRoot({66 hoists: [67 src_1.createSimpleExpression("hello", false, src_1.locStub),68 src_1.createObjectExpression([69 src_1.createObjectProperty(src_1.createSimpleExpression("id", true, src_1.locStub), src_1.createSimpleExpression("foo", true, src_1.locStub))70 ], src_1.locStub)71 ]72 });73 var code = src_1.generate(root).code;74 expect(code).toMatch("const _hoisted_1 = hello");75 expect(code).toMatch("const _hoisted_2 = { id: \"foo\" }");76 expect(code).toMatchSnapshot();77 });78 test('prefixIdentifiers: true should inject _ctx statement', function () {79 var code = src_1.generate(createRoot(), { prefixIdentifiers: true }).code;80 expect(code).toMatch("const _ctx = this\n");81 expect(code).toMatchSnapshot();82 });83 test('static text', function () {84 var code = src_1.generate(createRoot({85 codegenNode: {86 type: 2,87 content: 'hello',88 loc: src_1.locStub89 }90 })).code;91 expect(code).toMatch("return \"hello\"");92 expect(code).toMatchSnapshot();93 });94 test('interpolation', function () {95 var code = src_1.generate(createRoot({96 codegenNode: src_1.createInterpolation("hello", src_1.locStub)97 })).code;98 expect(code).toMatch("return _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.TO_STRING] + "(hello)");99 expect(code).toMatchSnapshot();100 });101 test('comment', function () {102 var code = src_1.generate(createRoot({103 codegenNode: {104 type: 3,105 content: 'foo',106 loc: src_1.locStub107 }108 })).code;109 expect(code).toMatch("return _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.CREATE_COMMENT] + "(\"foo\")");110 expect(code).toMatchSnapshot();111 });112 test('compound expression', function () {113 var code = src_1.generate(createRoot({114 codegenNode: src_1.createCompoundExpression([115 "_ctx.",116 src_1.createSimpleExpression("foo", false, src_1.locStub),117 " + ",118 {119 type: 5,120 loc: src_1.locStub,121 content: src_1.createSimpleExpression("bar", false, src_1.locStub)122 }123 ])124 })).code;125 expect(code).toMatch("return _ctx.foo + _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.TO_STRING] + "(bar)");126 expect(code).toMatchSnapshot();127 });128 test('ifNode', function () {129 var code = src_1.generate(createRoot({130 codegenNode: {131 type: 9,132 loc: src_1.locStub,133 branches: [],134 codegenNode: src_1.createSequenceExpression([135 src_1.createSimpleExpression('foo', false),136 src_1.createSimpleExpression('bar', false)137 ])138 }139 })).code;140 expect(code).toMatch("return (foo, bar)");141 expect(code).toMatchSnapshot();142 });143 test('forNode', function () {144 var code = src_1.generate(createRoot({145 codegenNode: {146 type: 11,147 loc: src_1.locStub,148 source: src_1.createSimpleExpression('foo', false),149 valueAlias: undefined,150 keyAlias: undefined,151 objectIndexAlias: undefined,152 children: [],153 codegenNode: src_1.createSequenceExpression([154 src_1.createSimpleExpression('foo', false),155 src_1.createSimpleExpression('bar', false)156 ])157 }158 })).code;159 expect(code).toMatch("return (foo, bar)");160 expect(code).toMatchSnapshot();161 });162 test('Element (callExpression + objectExpression + TemplateChildNode[])', function () {163 var code = src_1.generate(createRoot({164 codegenNode: testUtils_1.createElementWithCodegen([165 "\"div\"",166 src_1.createObjectExpression([167 src_1.createObjectProperty(src_1.createSimpleExpression("id", true, src_1.locStub), src_1.createSimpleExpression("foo", true, src_1.locStub)),168 src_1.createObjectProperty(src_1.createSimpleExpression("prop", false, src_1.locStub), src_1.createSimpleExpression("bar", false, src_1.locStub)),169 src_1.createObjectProperty({170 type: 8,171 loc: src_1.locStub,172 children: [173 "foo + ",174 src_1.createSimpleExpression("bar", false, src_1.locStub)175 ]176 }, src_1.createSimpleExpression("bar", false, src_1.locStub))177 ], src_1.locStub),178 [179 testUtils_1.createElementWithCodegen([180 "\"p\"",181 src_1.createObjectExpression([182 src_1.createObjectProperty(src_1.createSimpleExpression("some-key", true, src_1.locStub), src_1.createSimpleExpression("foo", true, src_1.locStub))183 ], src_1.locStub)184 ])185 ],186 shared_1.PatchFlags.FULL_PROPS + ''187 ])188 })).code;189 expect(code).toMatch("\n return _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.CREATE_VNODE] + "(\"div\", {\n id: \"foo\",\n [prop]: bar,\n [foo + bar]: bar\n }, [\n _" + runtimeHelpers_1.helperNameMap[runtimeHelpers_1.CREATE_VNODE] + "(\"p\", { \"some-key\": \"foo\" })\n ], " + shared_1.PatchFlags.FULL_PROPS + ")");190 expect(code).toMatchSnapshot();191 });192 test('ArrayExpression', function () {193 var code = src_1.generate(createRoot({194 codegenNode: src_1.createArrayExpression([195 src_1.createSimpleExpression("foo", false),196 src_1.createCallExpression("bar", ["baz"])197 ])198 })).code;199 expect(code).toMatch("return [\n foo,\n bar(baz)\n ]");200 expect(code).toMatchSnapshot();201 });202 test('SequenceExpression', function () {203 var code = src_1.generate(createRoot({204 codegenNode: src_1.createSequenceExpression([205 src_1.createSimpleExpression("foo", false),206 src_1.createCallExpression("bar", ["baz"])207 ])208 })).code;209 expect(code).toMatch("return (foo, bar(baz))");210 expect(code).toMatchSnapshot();211 });212 test('ConditionalExpression', function () {213 var code = src_1.generate(createRoot({214 codegenNode: src_1.createConditionalExpression(src_1.createSimpleExpression("ok", false), src_1.createCallExpression("foo"), src_1.createConditionalExpression(src_1.createSimpleExpression("orNot", false), src_1.createCallExpression("bar"), src_1.createCallExpression("baz")))215 })).code;216 expect(code).toMatch("return ok\n ? foo()\n : orNot\n ? bar()\n : baz()");217 expect(code).toMatchSnapshot();218 });219 test('CacheExpression', function () {220 var code = src_1.generate(createRoot({221 cached: 1,222 codegenNode: src_1.createCacheExpression(1, src_1.createSimpleExpression("foo", false))223 }), {224 mode: 'module',225 prefixIdentifiers: true226 }).code;227 expect(code).toMatch("const _cache = _ctx.$cache");228 expect(code).toMatch("_cache[1] || (_cache[1] = foo)");229 expect(code).toMatchSnapshot();230 });231 test('CacheExpression w/ isVNode: true', function () {232 var code = src_1.generate(createRoot({233 cached: 1,234 codegenNode: src_1.createCacheExpression(1, src_1.createSimpleExpression("foo", false), true)235 }), {236 mode: 'module',237 prefixIdentifiers: true238 }).code;239 expect(code).toMatch("const _cache = _ctx.$cache");240 expect(code).toMatch("\n _cache[1] || (\n setBlockTracking(-1),\n _cache[1] = foo,\n setBlockTracking(1),\n _cache[1]\n )\n ".trim());241 expect(code).toMatchSnapshot();242 });...
ReactDOMRoot-test.js
Source:ReactDOMRoot-test.js
...27 });28 return;29 }30 it('renders children', () => {31 const root = ReactDOM.createRoot(container);32 root.render(<div>Hi</div>);33 Scheduler.unstable_flushAll();34 expect(container.textContent).toEqual('Hi');35 });36 it('warns if a callback parameter is provided to render', () => {37 const callback = jest.fn();38 const root = ReactDOM.createRoot(container);39 expect(() =>40 root.render(<div>Hi</div>, callback),41 ).toErrorDev(42 'render(...): does not support the second callback argument. ' +43 'To execute a side effect after rendering, declare it in a component body with useEffect().',44 {withoutStack: true},45 );46 Scheduler.unstable_flushAll();47 expect(callback).not.toHaveBeenCalled();48 });49 it('warns if a callback parameter is provided to unmount', () => {50 const callback = jest.fn();51 const root = ReactDOM.createRoot(container);52 root.render(<div>Hi</div>);53 expect(() =>54 root.unmount(callback),55 ).toErrorDev(56 'unmount(...): does not support a callback argument. ' +57 'To execute a side effect after rendering, declare it in a component body with useEffect().',58 {withoutStack: true},59 );60 Scheduler.unstable_flushAll();61 expect(callback).not.toHaveBeenCalled();62 });63 it('unmounts children', () => {64 const root = ReactDOM.createRoot(container);65 root.render(<div>Hi</div>);66 Scheduler.unstable_flushAll();67 expect(container.textContent).toEqual('Hi');68 root.unmount();69 Scheduler.unstable_flushAll();70 expect(container.textContent).toEqual('');71 });72 it('supports hydration', async () => {73 const markup = await new Promise(resolve =>74 resolve(75 ReactDOMServer.renderToString(76 <div>77 <span className="extra" />78 </div>,79 ),80 ),81 );82 // Does not hydrate by default83 const container1 = document.createElement('div');84 container1.innerHTML = markup;85 const root1 = ReactDOM.createRoot(container1);86 root1.render(87 <div>88 <span />89 </div>,90 );91 Scheduler.unstable_flushAll();92 // Accepts `hydrate` option93 const container2 = document.createElement('div');94 container2.innerHTML = markup;95 const root2 = ReactDOM.createRoot(container2, {hydrate: true});96 root2.render(97 <div>98 <span />99 </div>,100 );101 expect(() => Scheduler.unstable_flushAll()).toErrorDev('Extra attributes');102 });103 it('does not clear existing children', async () => {104 container.innerHTML = '<div>a</div><div>b</div>';105 const root = ReactDOM.createRoot(container);106 root.render(107 <div>108 <span>c</span>109 <span>d</span>110 </div>,111 );112 Scheduler.unstable_flushAll();113 expect(container.textContent).toEqual('abcd');114 root.render(115 <div>116 <span>d</span>117 <span>c</span>118 </div>,119 );120 Scheduler.unstable_flushAll();121 expect(container.textContent).toEqual('abdc');122 });123 it('throws a good message on invalid containers', () => {124 expect(() => {125 ReactDOM.createRoot(<div>Hi</div>);126 }).toThrow('createRoot(...): Target container is not a DOM element.');127 });128 it('warns when rendering with legacy API into createRoot() container', () => {129 const root = ReactDOM.createRoot(container);130 root.render(<div>Hi</div>);131 Scheduler.unstable_flushAll();132 expect(container.textContent).toEqual('Hi');133 expect(() => {134 ReactDOM.render(<div>Bye</div>, container);135 }).toErrorDev(136 [137 // We care about this warning:138 'You are calling ReactDOM.render() on a container that was previously ' +139 'passed to ReactDOM.createRoot(). This is not supported. ' +140 'Did you mean to call root.render(element)?',141 // This is more of a symptom but restructuring the code to avoid it isn't worth it:142 'Replacing React-rendered children with a new root component.',143 ],144 {withoutStack: true},145 );146 Scheduler.unstable_flushAll();147 // This works now but we could disallow it:148 expect(container.textContent).toEqual('Bye');149 });150 it('warns when hydrating with legacy API into createRoot() container', () => {151 const root = ReactDOM.createRoot(container);152 root.render(<div>Hi</div>);153 Scheduler.unstable_flushAll();154 expect(container.textContent).toEqual('Hi');155 expect(() => {156 ReactDOM.hydrate(<div>Hi</div>, container);157 }).toErrorDev(158 [159 // We care about this warning:160 'You are calling ReactDOM.hydrate() on a container that was previously ' +161 'passed to ReactDOM.createRoot(). This is not supported. ' +162 'Did you mean to call createRoot(container, {hydrate: true}).render(element)?',163 // This is more of a symptom but restructuring the code to avoid it isn't worth it:164 'Replacing React-rendered children with a new root component.',165 ],166 {withoutStack: true},167 );168 });169 it('warns when unmounting with legacy API (no previous content)', () => {170 const root = ReactDOM.createRoot(container);171 root.render(<div>Hi</div>);172 Scheduler.unstable_flushAll();173 expect(container.textContent).toEqual('Hi');174 let unmounted = false;175 expect(() => {176 unmounted = ReactDOM.unmountComponentAtNode(container);177 }).toErrorDev(178 [179 // We care about this warning:180 'You are calling ReactDOM.unmountComponentAtNode() on a container that was previously ' +181 'passed to ReactDOM.createRoot(). This is not supported. Did you mean to call root.unmount()?',182 // This is more of a symptom but restructuring the code to avoid it isn't worth it:183 "The node you're attempting to unmount was rendered by React and is not a top-level container.",184 ],185 {withoutStack: true},186 );187 expect(unmounted).toBe(false);188 Scheduler.unstable_flushAll();189 expect(container.textContent).toEqual('Hi');190 root.unmount();191 Scheduler.unstable_flushAll();192 expect(container.textContent).toEqual('');193 });194 it('warns when unmounting with legacy API (has previous content)', () => {195 // Currently createRoot().render() doesn't clear this.196 container.appendChild(document.createElement('div'));197 // The rest is the same as test above.198 const root = ReactDOM.createRoot(container);199 root.render(<div>Hi</div>);200 Scheduler.unstable_flushAll();201 expect(container.textContent).toEqual('Hi');202 let unmounted = false;203 expect(() => {204 unmounted = ReactDOM.unmountComponentAtNode(container);205 }).toErrorDev('Did you mean to call root.unmount()?', {withoutStack: true});206 expect(unmounted).toBe(false);207 Scheduler.unstable_flushAll();208 expect(container.textContent).toEqual('Hi');209 root.unmount();210 Scheduler.unstable_flushAll();211 expect(container.textContent).toEqual('');212 });213 it('warns when passing legacy container to createRoot()', () => {214 ReactDOM.render(<div>Hi</div>, container);215 expect(() => {216 ReactDOM.createRoot(container);217 }).toErrorDev(218 'You are calling ReactDOM.createRoot() on a container that was previously ' +219 'passed to ReactDOM.render(). This is not supported.',220 {withoutStack: true},221 );222 });223 it('warns when creating two roots managing the same container', () => {224 ReactDOM.createRoot(container);225 expect(() => {226 ReactDOM.createRoot(container);227 }).toErrorDev(228 'You are calling ReactDOM.createRoot() on a container that ' +229 'has already been passed to createRoot() before. Instead, call ' +230 'root.render() on the existing root instead if you want to update it.',231 {withoutStack: true},232 );233 });234 it('does not warn when creating second root after first one is unmounted', () => {235 const root = ReactDOM.createRoot(container);236 root.unmount();237 Scheduler.unstable_flushAll();238 ReactDOM.createRoot(container); // No warning239 });240 it('warns if creating a root on the document.body', async () => {241 expect(() => {242 ReactDOM.createRoot(document.body);243 }).toErrorDev(244 'createRoot(): Creating roots directly with document.body is ' +245 'discouraged, since its children are often manipulated by third-party ' +246 'scripts and browser extensions. This may lead to subtle ' +247 'reconciliation issues. Try using a container element created ' +248 'for your app.',249 {withoutStack: true},250 );251 });252 it('warns if updating a root that has had its contents removed', async () => {253 const root = ReactDOM.createRoot(container);254 root.render(<div>Hi</div>);255 Scheduler.unstable_flushAll();256 container.innerHTML = '';257 expect(() => {258 root.render(<div>Hi</div>);259 }).toErrorDev(260 'render(...): It looks like the React-rendered content of the ' +261 'root container was removed without using React. This is not ' +262 'supported and will cause errors. Instead, call ' +263 "root.unmount() to empty a root's container.",264 {withoutStack: true},265 );266 });267});
ReactDOMRoot-test.internal.js
Source:ReactDOMRoot-test.internal.js
...64 ReactDOMServer = require('react-dom/server');65 AsyncMode = React.unstable_AsyncMode;66 });67 it('renders children', () => {68 const root = ReactDOM.createRoot(container);69 root.render(<div>Hi</div>);70 flush();71 expect(container.textContent).toEqual('Hi');72 });73 it('unmounts children', () => {74 const root = ReactDOM.createRoot(container);75 root.render(<div>Hi</div>);76 flush();77 expect(container.textContent).toEqual('Hi');78 root.unmount();79 flush();80 expect(container.textContent).toEqual('');81 });82 it('`root.render` returns a thenable work object', () => {83 const root = ReactDOM.createRoot(container);84 const work = root.render(<AsyncMode>Hi</AsyncMode>);85 let ops = [];86 work.then(() => {87 ops.push('inside callback: ' + container.textContent);88 });89 ops.push('before committing: ' + container.textContent);90 flush();91 ops.push('after committing: ' + container.textContent);92 expect(ops).toEqual([93 'before committing: ',94 // `then` callback should fire during commit phase95 'inside callback: Hi',96 'after committing: Hi',97 ]);98 });99 it('resolves `work.then` callback synchronously if the work already committed', () => {100 const root = ReactDOM.createRoot(container);101 const work = root.render(<AsyncMode>Hi</AsyncMode>);102 flush();103 let ops = [];104 work.then(() => {105 ops.push('inside callback');106 });107 expect(ops).toEqual(['inside callback']);108 });109 it('supports hydration', async () => {110 const markup = await new Promise(resolve =>111 resolve(112 ReactDOMServer.renderToString(113 <div>114 <span className="extra" />115 </div>,116 ),117 ),118 );119 // Does not hydrate by default120 const container1 = document.createElement('div');121 container1.innerHTML = markup;122 const root1 = ReactDOM.createRoot(container1);123 root1.render(124 <div>125 <span />126 </div>,127 );128 flush();129 // Accepts `hydrate` option130 const container2 = document.createElement('div');131 container2.innerHTML = markup;132 const root2 = ReactDOM.createRoot(container2, {hydrate: true});133 root2.render(134 <div>135 <span />136 </div>,137 );138 expect(flush).toWarnDev('Extra attributes');139 });140 it('does not clear existing children', async () => {141 container.innerHTML = '<div>a</div><div>b</div>';142 const root = ReactDOM.createRoot(container);143 root.render(144 <div>145 <span>c</span>146 <span>d</span>147 </div>,148 );149 flush();150 expect(container.textContent).toEqual('abcd');151 root.render(152 <div>153 <span>d</span>154 <span>c</span>155 </div>,156 );157 flush();158 expect(container.textContent).toEqual('abdc');159 });160 it('can defer a commit by batching it', () => {161 const root = ReactDOM.createRoot(container);162 const batch = root.createBatch();163 batch.render(<div>Hi</div>);164 // Hasn't committed yet165 expect(container.textContent).toEqual('');166 // Commit167 batch.commit();168 expect(container.textContent).toEqual('Hi');169 });170 it('does not restart a completed batch when committing if there were no intervening updates', () => {171 let ops = [];172 function Foo(props) {173 ops.push('Foo');174 return props.children;175 }176 const root = ReactDOM.createRoot(container);177 const batch = root.createBatch();178 batch.render(<Foo>Hi</Foo>);179 // Flush all async work.180 flush();181 // Root should complete without committing.182 expect(ops).toEqual(['Foo']);183 expect(container.textContent).toEqual('');184 ops = [];185 // Commit. Shouldn't re-render Foo.186 batch.commit();187 expect(ops).toEqual([]);188 expect(container.textContent).toEqual('Hi');189 });190 it('can wait for a batch to finish', () => {191 const root = ReactDOM.createRoot(container);192 const batch = root.createBatch();193 batch.render(<AsyncMode>Foo</AsyncMode>);194 flush();195 // Hasn't updated yet196 expect(container.textContent).toEqual('');197 let ops = [];198 batch.then(() => {199 // Still hasn't updated200 ops.push(container.textContent);201 // Should synchronously commit202 batch.commit();203 ops.push(container.textContent);204 });205 expect(ops).toEqual(['', 'Foo']);206 });207 it('`batch.render` returns a thenable work object', () => {208 const root = ReactDOM.createRoot(container);209 const batch = root.createBatch();210 const work = batch.render('Hi');211 let ops = [];212 work.then(() => {213 ops.push('inside callback: ' + container.textContent);214 });215 ops.push('before committing: ' + container.textContent);216 batch.commit();217 ops.push('after committing: ' + container.textContent);218 expect(ops).toEqual([219 'before committing: ',220 // `then` callback should fire during commit phase221 'inside callback: Hi',222 'after committing: Hi',223 ]);224 });225 it('can commit an empty batch', () => {226 const root = ReactDOM.createRoot(container);227 root.render(<AsyncMode>1</AsyncMode>);228 expire(2000);229 // This batch has a later expiration time than the earlier update.230 const batch = root.createBatch();231 // This should not flush the earlier update.232 batch.commit();233 expect(container.textContent).toEqual('');234 flush();235 expect(container.textContent).toEqual('1');236 });237 it('two batches created simultaneously are committed separately', () => {238 // (In other words, they have distinct expiration times)239 const root = ReactDOM.createRoot(container);240 const batch1 = root.createBatch();241 batch1.render(1);242 const batch2 = root.createBatch();243 batch2.render(2);244 expect(container.textContent).toEqual('');245 batch1.commit();246 expect(container.textContent).toEqual('1');247 batch2.commit();248 expect(container.textContent).toEqual('2');249 });250 it('commits an earlier batch without committing a later batch', () => {251 const root = ReactDOM.createRoot(container);252 const batch1 = root.createBatch();253 batch1.render(1);254 // This batch has a later expiration time255 expire(2000);256 const batch2 = root.createBatch();257 batch2.render(2);258 expect(container.textContent).toEqual('');259 batch1.commit();260 expect(container.textContent).toEqual('1');261 batch2.commit();262 expect(container.textContent).toEqual('2');263 });264 it('commits a later batch without committing an earlier batch', () => {265 const root = ReactDOM.createRoot(container);266 const batch1 = root.createBatch();267 batch1.render(1);268 // This batch has a later expiration time269 expire(2000);270 const batch2 = root.createBatch();271 batch2.render(2);272 expect(container.textContent).toEqual('');273 batch2.commit();274 expect(container.textContent).toEqual('2');275 batch1.commit();276 flush();277 expect(container.textContent).toEqual('1');278 });279});
client.js
Source:client.js
...16 400,17 Surface.SurfaceShape.Cylinder18 );19 introRoot = r360.renderToSurface(20 r360.createRoot('TourismAppVR', {}),21 introPanel22 );23 marketPanel = new Surface(24 100,25 100,26 Surface.SurfaceShape.Flat27 )28 marketPanel.setAngle(29 0.2,30 031 );32 museumPanel = new Surface(33 100,34 100,35 Surface.SurfaceShape.Flat36 )37 museumPanel.setAngle(38 Math.PI / 2,39 040 );41 restaurantPanel = new Surface(42 100,43 100,44 Surface.SurfaceShape.Flat45 )46 restaurantPanel.setAngle(47 -Math.PI / 2,48 049 );50 shoppingPanel = new Surface(51 100,52 100,53 Surface.SurfaceShape.Flat54 );55 /* r360.renderToSurface(56 r360.createRoot('InfoPanel',{}),57 marketPanel58 );59 r360.renderToSurface(60 r360.createRoot('InfoPanel',{}),61 shoppingPanel62 );63 r360.renderToSurface(64 r360.createRoot('InfoPanel',{}),65 museumPanel66 );67 r360.renderToSurface(68 r360.createRoot('InfoPanel',{}),69 restaurantPanel70 ); */71 shoppingPanel.setAngle(72 3.6,73 074 );75 r360.compositor.setBackground(r360.getAssetURL('gdansk.jpg'));76}77class surfaceModule extends Module{78 constructor(){79 super('surfaceModule');80 }81 resizeSurface(width, height, id){82 if(id === 'museum'){83 museumPanel.resize(width, height);84 } else if (id === 'restaurant'){85 restaurantPanel.resize(width, height);86 } else if (id === 'shopping'){87 shoppingPanel.resize(width, height);88 } else if (id === 'market'){89 marketPanel.resize(width, height);90 }91 }92 start(){93 r360.renderToSurface(94 r360.createRoot('InfoPanel',{id:'market', text: 'Browse our incredible market' }),95 marketPanel96 );97 98 r360.renderToSurface(99 r360.createRoot('InfoPanel',{id:'shopping', text: 'Shop until you drop!'}),100 shoppingPanel101 );102 103 r360.renderToSurface(104 r360.createRoot('InfoPanel',{id:'museum', text: 'The Life of Pablo Piccasso: Blue.'}),105 museumPanel106 );107 108 r360.renderToSurface(109 r360.createRoot('InfoPanel',{id:'restaurant', text: 'Enjoy a delecious beer at our restaurants. '}),110 restaurantPanel111 );112 r360.detachRoot(introRoot);113 }114}...
Using AI Code Generation
1const playwright = require('playwright');2(async () => {3 for (const browserType of ['chromium', 'firefox', 'webkit']) {4 const browser = await playwright[browserType].launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const root = await page.createRoot();8 await root.innerHTML = '<div> Hello World </div>';9 await page.screenshot({ path: `example-${browserType}.png` });10 await browser.close();11 }12})();13const playwright = require('playwright');14(async () => {15 for (const browserType of ['chromium', 'firefox', 'webkit']) {16 const browser = await playwright[browserType].launch();17 const context = await browser.newContext();18 const page = await context.newPage();19 const root = await page.createRoot();20 await root.innerHTML = '<div> Hello World </div>';21 await page.screenshot({ path: `example-${browserType}.png` });22 await browser.close();23 }24})();25const playwright = require('playwright');26(async () => {27 for (const browserType of ['chromium', 'firefox', 'webkit']) {28 const browser = await playwright[browserType].launch();29 const context = await browser.newContext();30 const page = await context.newPage();31 const root = await page.createRoot();32 await root.innerHTML = '<div> Hello World </div>';33 await page.screenshot({ path: `example-${browserType}.png` });34 await browser.close();35 }36})();37const playwright = require('playwright');38(async () => {39 for (const browserType of ['chromium', 'firefox', 'webkit']) {40 const browser = await playwright[browserType].launch();41 const context = await browser.newContext();42 const page = await context.newPage();43 const root = await page.createRoot();44 await root.innerHTML = '<div> Hello World </div>';45 await page.screenshot({ path: `example-${browserType}.png` });46 await browser.close();47 }48})();
Using AI Code Generation
1const playwright = require('playwright');2(async () => {3 const browser = await playwright.chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 const root = await page.createRoot();7 await root.innerHTML = '<div>Root</div>';8 await browser.close();9})();
Using AI Code Generation
1const { createRoot } = require('playwright/lib/server/browserContext');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const root = await createRoot(context);7 const page = await root.newPage();8 await page.screenshot({ path: 'example.png' });9 await browser.close();10})();11{12 "scripts": {13 },14 "dependencies": {15 }16}
Using AI Code Generation
1const { createRoot } = require('playwright/lib/server/browserContext');2const { BrowserContext } = require('playwright/lib/server/browserContext');3const { Page } = require('playwright/lib/server/page');4const { Frame } = require('playwright/lib/server/frame');5const { ElementHandle } = require('playwright/lib/server/dom');6const root = createRoot();7const context = new BrowserContext(root, null, {});8const page = new Page(root, context, {}, null);9const frame = new Frame(root, page, null, {});10const element = new ElementHandle(root, frame, null, {});11console.log(element);12ElementHandle {13 _context: BrowserContext {14 _browser: Browser {15 _connection: Connection {
Using AI Code Generation
1const playwright = require('playwright');2(async () => {3 const browser = await playwright.chromium.launch();4 const page = await browser.newPage();5 const root = await page.createRoot();6 await root.innerHTML = `<h1>Hello, world!</h1>`;7 await page.screenshot({ path: `example.png` });8 await browser.close();9})();10const playwright = require('playwright');11const fs = require('fs');12const path = require('path');13const assert = require('assert');14const { PNG } = require('pngjs');15const pixelmatch = require('pixelmatch');16(async () => {17 const browser = await playwright.chromium.launch();18 const page = await browser.newPage();19 const root = await page.createRoot();20 await root.innerHTML = fs.readFileSync(path.join(__dirname, 'test.html'), 'utf8');21 const screenshot = await page.screenshot();22 const img1 = PNG.sync.read(screenshot);23 const img2 = PNG.sync.read(fs.readFileSync(path.join(__dirname, 'example.png')));24 const { width, height } = img1;25 const diff = new PNG({ width, height });26 const numDiffPixels = pixelmatch(img1.data, img2.data, diff.data, width, height, { threshold: 0.1 });27 assert.strictEqual(numDiffPixels, 0);28 await browser.close();29})();
Using AI Code Generation
1const { createRoot } = require('playwright/lib/server/browserType');2const { Playwright } = require('playwright');3const playwright = new Playwright();4const browserType = playwright.chromium;5const root = createRoot(browserType);6(async () => {7 const browserServer = await root.launchServer({8 });9 console.log(`Server running on ${browserServer.wsEndpoint()}`);10})();
Using AI Code Generation
1const { chromium } = require('playwright');2const { createRoot } = require('playwright/lib/server/browserContext');3(async () => {4 const browser = await chromium.launch();5 const root = await createRoot(browser._browserContext);6 const page = await root.newPage();7 await page.screenshot({ path: 'example.png' });8 await browser.close();9})();
Using AI Code Generation
1const { createRoot } = require('playwright/lib/server/chromium');2const { createServer } = require('playwright/lib/server/chromium');3const { createBrowserContext } = require('playwright/lib/server/chromium');4const { createPage } = require('playwright/lib/server/chromium');5const { createFrame } = require('playwright/lib/server/chromium');6const { createRoot } = require('playwright/lib/server/firefox');7const { createServer } = require('playwright/lib/server/firefox');8const { createBrowserContext } = require('playwright/lib/server/firefox');9const { createPage } = require('playwright/lib/server/firefox');10const { createFrame } = require('playwright/lib/server/firefox');11const { createRoot } = require('playwright/lib/server/webkit');12const { createServer } = require('playwright/lib/server/webkit');13const { createBrowserContext } = require('playwright/lib/server/webkit');14const { createPage } = require('playwright/lib/server/webkit');15const { createFrame } = require('playwright/lib/server/webkit');16const { createRoot } = require('playwright/lib/server/android');17const { createServer } = require('playwright/lib/server/android');18const { createBrowserContext } = require('playwright/lib/server/android');
Using AI Code Generation
1const playwright = require('playwright');2const { createRoot } = playwright.internal;3const root = createRoot();4(async () => {5 const browser = await root.launchBrowser();6 const context = await browser.newContext();7 const page = await context.newPage();8 await page.screenshot({ path: 'example.png' });9 await browser.close();10})();11const playwright = require('playwright');12const { createRoot } = playwright.internal;13const root = createRoot();14module.exports = root;
Using AI Code Generation
1const playwright = require('playwright');2const { createRoot } = playwright;3const root = createRoot();4const page = await root.newPage();5const playwright = require('playwright');6const { createRoot } = playwright;7const root = createRoot();8const page = await root.newPage();9const playwright = require('playwright');10const { createRoot } = playwright;11const root = createRoot();12const page = await root.newPage();13const playwright = require('playwright');14const { createRoot } = playwright;15const root = createRoot();16const page = await root.newPage();17const playwright = require('playwright');18const { createRoot } = playwright;19const root = createRoot();20const page = await root.newPage();21const playwright = require('playwright');22const { createRoot } = playwright;23const root = createRoot();24const page = await root.newPage();25const playwright = require('playwright');26const { createRoot } = playwright;27const root = createRoot();28const page = await root.newPage();29const playwright = require('playwright');30const { createRoot } = playwright;31const root = createRoot();32const page = await root.newPage();33const playwright = require('playwright');34const { createRoot } = playwright;35const root = createRoot();36const page = await root.newPage();37const playwright = require('playwright');38const { createRoot } = playwright;39const root = createRoot();40const page = await root.newPage();41const playwright = require('playwright');42const { createRoot } = playwright;43const root = createRoot();44const page = await root.newPage();45const playwright = require('playwright');46const { createRoot } = playwright;47const root = createRoot();
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!