Best JavaScript code snippet using fast-check-monorepo
dynamic.js
Source:dynamic.js
1// This file is part of Moodle - http://moodle.org/2//3// Moodle is free software: you can redistribute it and/or modify4// it under the terms of the GNU General Public License as published by5// the Free Software Foundation, either version 3 of the License, or6// (at your option) any later version.7//8// Moodle is distributed in the hope that it will be useful,9// but WITHOUT ANY WARRANTY; without even the implied warranty of10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11// GNU General Public License for more details.12//13// You should have received a copy of the GNU General Public License14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.15/**16 * Module to handle dynamic table features.17 *18 * @module core_table/dynamic19 * @package core_table20 * @copyright 2020 Simey Lameze <simey@moodle.com>21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later22 */23import * as Selectors from 'core_table/local/dynamic/selectors';24import Events from './local/dynamic/events';25import Pending from 'core/pending';26import {addIconToContainer} from 'core/loadingicon';27import {fetch as fetchTableData} from 'core_table/local/dynamic/repository';28import Notification from 'core/notification';29let watching = false;30/**31 * Ensure that a table is a dynamic table.32 *33 * @param {HTMLElement} tableRoot34 * @returns {Bool}35 */36const checkTableIsDynamic = tableRoot => {37 if (!tableRoot) {38 // The table is not a dynamic table.39 throw new Error("The table specified is not a dynamic table and cannot be updated");40 }41 if (!tableRoot.matches(Selectors.main.region)) {42 // The table is not a dynamic table.43 throw new Error("The table specified is not a dynamic table and cannot be updated");44 }45 return true;46};47/**48 * Get the filterset data from a known dynamic table.49 *50 * @param {HTMLElement} tableRoot51 * @returns {Object}52 */53const getFiltersetFromTable = tableRoot => {54 return JSON.parse(tableRoot.dataset.tableFilters);55};56/**57 * Update the specified table based on its current values.58 *59 * @param {HTMLElement} tableRoot60 * @param {Bool} resetContent61 * @returns {Promise}62 */63export const refreshTableContent = (tableRoot, resetContent = false) => {64 const filterset = getFiltersetFromTable(tableRoot);65 addIconToContainer(tableRoot);66 const pendingPromise = new Pending('core_table/dynamic:refreshTableContent');67 return fetchTableData(68 tableRoot.dataset.tableComponent,69 tableRoot.dataset.tableHandler,70 tableRoot.dataset.tableUniqueid,71 {72 sortData: JSON.parse(tableRoot.dataset.tableSortData),73 joinType: filterset.jointype,74 filters: filterset.filters,75 firstinitial: tableRoot.dataset.tableFirstInitial,76 lastinitial: tableRoot.dataset.tableLastInitial,77 pageNumber: tableRoot.dataset.tablePageNumber,78 pageSize: tableRoot.dataset.tablePageSize,79 hiddenColumns: JSON.parse(tableRoot.dataset.tableHiddenColumns),80 },81 resetContent,82 )83 .then(data => {84 const placeholder = document.createElement('div');85 placeholder.innerHTML = data.html;86 tableRoot.replaceWith(...placeholder.childNodes);87 // Update the tableRoot.88 return getTableFromId(tableRoot.dataset.tableUniqueid);89 }).then(tableRoot => {90 tableRoot.dispatchEvent(new CustomEvent(Events.tableContentRefreshed, {91 bubbles: true,92 }));93 return tableRoot;94 })95 .then(tableRoot => {96 pendingPromise.resolve();97 return tableRoot;98 });99};100export const updateTable = (tableRoot, {101 sortBy = null,102 sortOrder = null,103 filters = null,104 firstInitial = null,105 lastInitial = null,106 pageNumber = null,107 pageSize = null,108 hiddenColumns = null,109} = {}, refreshContent = true) => {110 checkTableIsDynamic(tableRoot);111 const pendingPromise = new Pending('core_table/dynamic:updateTable');112 let tableConfigChanged = false;113 // Update sort fields.114 if (sortBy && sortOrder) {115 // Always update the table if requested and there were sort fields.116 // These fields are only ever normalised in the backend.117 tableConfigChanged = true;118 const sortData = JSON.parse(tableRoot.dataset.tableSortData);119 sortData.unshift({120 sortby: sortBy,121 sortorder: parseInt(sortOrder, 10),122 });123 tableRoot.dataset.tableSortData = JSON.stringify(sortData);124 }125 // Update initials.126 if (firstInitial !== null) {127 if (tableRoot.dataset.tableFirstInitial !== firstInitial) {128 tableConfigChanged = true;129 }130 tableRoot.dataset.tableFirstInitial = firstInitial;131 }132 if (lastInitial !== null) {133 if (tableRoot.dataset.tableLastInitial !== lastInitial) {134 tableConfigChanged = true;135 }136 tableRoot.dataset.tableLastInitial = lastInitial;137 }138 if (pageSize !== null) {139 if (tableRoot.dataset.tablePageSize != pageSize) {140 tableConfigChanged = true;141 }142 tableRoot.dataset.tablePageSize = pageSize;143 }144 // Update filters.145 if (filters) {146 const filterJson = JSON.stringify(filters);147 if (tableRoot.dataset.tableFilters !== filterJson) {148 tableConfigChanged = true;149 }150 tableRoot.dataset.tableFilters = filterJson;151 }152 // Reset to page 1 when table content is being altered by filtering or sorting.153 // This ensures the table page being loaded always exists, and gives a consistent experience.154 if (tableConfigChanged) {155 pageNumber = 1;156 }157 // Update hidden columns.158 if (hiddenColumns) {159 const columnJson = JSON.stringify(hiddenColumns);160 if (tableRoot.dataset.tableHiddenColumns !== columnJson) {161 tableConfigChanged = true;162 }163 tableRoot.dataset.tableHiddenColumns = columnJson;164 }165 if (pageNumber !== null) {166 if (tableRoot.dataset.tablePageNumber != pageNumber) {167 tableConfigChanged = true;168 }169 tableRoot.dataset.tablePageNumber = pageNumber;170 }171 // Refresh.172 if (refreshContent && tableConfigChanged) {173 return refreshTableContent(tableRoot)174 .then(tableRoot => {175 pendingPromise.resolve();176 return tableRoot;177 });178 } else {179 pendingPromise.resolve();180 return Promise.resolve(tableRoot);181 }182};183/**184 * Get the table dataset for the specified tableRoot, ensuring that the provided table is a dynamic table.185 *186 * @param {HTMLElement} tableRoot187 * @returns {DOMStringMap}188 */189const getTableData = tableRoot => {190 checkTableIsDynamic(tableRoot);191 return tableRoot.dataset;192};193/**194 * Update the specified table using the new filters.195 *196 * @param {HTMLElement} tableRoot197 * @param {Object} filters198 * @param {Bool} refreshContent199 * @returns {Promise}200 */201export const setFilters = (tableRoot, filters, refreshContent = true) =>202 updateTable(tableRoot, {filters}, refreshContent);203/**204 * Get the filter data for the specified table.205 *206 * @param {HTMLElement} tableRoot207 * @returns {Object}208 */209export const getFilters = tableRoot => {210 checkTableIsDynamic(tableRoot);211 return getFiltersetFromTable(tableRoot);212};213/**214 * Update the sort order.215 *216 * @param {HTMLElement} tableRoot217 * @param {String} sortBy218 * @param {Number} sortOrder219 * @param {Bool} refreshContent220 * @returns {Promise}221 */222export const setSortOrder = (tableRoot, sortBy, sortOrder, refreshContent = true) =>223 updateTable(tableRoot, {sortBy, sortOrder}, refreshContent);224/**225 * Set the page number.226 *227 * @param {HTMLElement} tableRoot228 * @param {String} pageNumber229 * @param {Bool} refreshContent230 * @returns {Promise}231 */232export const setPageNumber = (tableRoot, pageNumber, refreshContent = true) =>233 updateTable(tableRoot, {pageNumber}, refreshContent);234/**235 * Get the current page number.236 *237 * @param {HTMLElement} tableRoot238 * @returns {Number}239 */240export const getPageNumber = tableRoot => getTableData(tableRoot).tablePageNumber;241/**242 * Set the page size.243 *244 * @param {HTMLElement} tableRoot245 * @param {Number} pageSize246 * @param {Bool} refreshContent247 * @returns {Promise}248 */249export const setPageSize = (tableRoot, pageSize, refreshContent = true) =>250 updateTable(tableRoot, {pageSize, pageNumber: 1}, refreshContent);251/**252 * Get the current page size.253 *254 * @param {HTMLElement} tableRoot255 * @returns {Number}256 */257export const getPageSize = tableRoot => getTableData(tableRoot).tablePageSize;258/**259 * Update the first initial to show.260 *261 * @param {HTMLElement} tableRoot262 * @param {String} firstInitial263 * @param {Bool} refreshContent264 * @returns {Promise}265 */266export const setFirstInitial = (tableRoot, firstInitial, refreshContent = true) =>267 updateTable(tableRoot, {firstInitial}, refreshContent);268/**269 * Get the current first initial filter.270 *271 * @param {HTMLElement} tableRoot272 * @returns {String}273 */274export const getFirstInitial = tableRoot => getTableData(tableRoot).tableFirstInitial;275/**276 * Update the last initial to show.277 *278 * @param {HTMLElement} tableRoot279 * @param {String} lastInitial280 * @param {Bool} refreshContent281 * @returns {Promise}282 */283export const setLastInitial = (tableRoot, lastInitial, refreshContent = true) =>284 updateTable(tableRoot, {lastInitial}, refreshContent);285/**286 * Get the current last initial filter.287 *288 * @param {HTMLElement} tableRoot289 * @returns {String}290 */291export const getLastInitial = tableRoot => getTableData(tableRoot).tableLastInitial;292/**293 * Hide a column in the participants table.294 *295 * @param {HTMLElement} tableRoot296 * @param {String} columnToHide297 * @param {Bool} refreshContent298 * @returns {Promise}299 */300export const hideColumn = (tableRoot, columnToHide, refreshContent = true) => {301 const hiddenColumns = JSON.parse(tableRoot.dataset.tableHiddenColumns);302 hiddenColumns.push(columnToHide);303 return updateTable(tableRoot, {hiddenColumns}, refreshContent);304};305/**306 * Make a hidden column visible in the participants table.307 *308 * @param {HTMLElement} tableRoot309 * @param {String} columnToShow310 * @param {Bool} refreshContent311 * @returns {Promise}312 */313export const showColumn = (tableRoot, columnToShow, refreshContent = true) => {314 let hiddenColumns = JSON.parse(tableRoot.dataset.tableHiddenColumns);315 hiddenColumns = hiddenColumns.filter(columnName => columnName !== columnToShow);316 return updateTable(tableRoot, {hiddenColumns}, refreshContent);317};318/**319 * Reset table preferences.320 *321 * @param {HTMLElement} tableRoot322 * @returns {Promise}323 */324const resetTablePreferences = tableRoot => refreshTableContent(tableRoot, true);325/**326 * Set up listeners to handle table updates.327 */328export const init = () => {329 if (watching) {330 // Already watching.331 return;332 }333 watching = true;334 document.addEventListener('click', e => {335 const tableRoot = e.target.closest(Selectors.main.region);336 if (!tableRoot) {337 return;338 }339 const sortableLink = e.target.closest(Selectors.table.links.sortableColumn);340 if (sortableLink) {341 e.preventDefault();342 setSortOrder(tableRoot, sortableLink.dataset.sortby, sortableLink.dataset.sortorder)343 .catch(Notification.exception);344 }345 const firstInitialLink = e.target.closest(Selectors.initialsBar.links.firstInitial);346 if (firstInitialLink !== null) {347 e.preventDefault();348 setFirstInitial(tableRoot, firstInitialLink.dataset.initial).catch(Notification.exception);349 }350 const lastInitialLink = e.target.closest(Selectors.initialsBar.links.lastInitial);351 if (lastInitialLink !== null) {352 e.preventDefault();353 setLastInitial(tableRoot, lastInitialLink.dataset.initial).catch(Notification.exception);354 }355 const pageItem = e.target.closest(Selectors.paginationBar.links.pageItem);356 if (pageItem) {357 e.preventDefault();358 setPageNumber(tableRoot, pageItem.dataset.pageNumber).catch(Notification.exception);359 }360 const hide = e.target.closest(Selectors.table.links.hide);361 if (hide) {362 e.preventDefault();363 hideColumn(tableRoot, hide.dataset.column).catch(Notification.exception);364 }365 const show = e.target.closest(Selectors.table.links.show);366 if (show) {367 e.preventDefault();368 showColumn(tableRoot, show.dataset.column).catch(Notification.exception);369 }370 const resetTablePreferencesLink = e.target.closest('.resettable a');371 if (resetTablePreferencesLink) {372 e.preventDefault();373 resetTablePreferences(tableRoot).catch(Notification.exception);374 }375 });376};377/**378 * Fetch the table via its table region id.379 *380 * @param {String} tableRegionId381 * @returns {HTMLElement}382 */383export const getTableFromId = tableRegionId => {384 const tableRoot = document.querySelector(Selectors.main.fromRegionId(tableRegionId));385 if (!tableRoot) {386 // The table is not a dynamic table.387 throw new Error("The table specified is not a dynamic table and cannot be updated");388 }389 return tableRoot;390};391export {392 Events...
Using AI Code Generation
1const fastCheckMonorepo = require('fast-check-monorepo');2fastCheckMonorepo.refreshContent();3const fastCheckMonorepo = require('fast-check-monorepo');4fastCheckMonorepo.refreshContent();5const fastCheckMonorepo = require('fast-check-monorepo');6fastCheckMonorepo.refreshContent();7const fastCheckMonorepo = require('fast-check-monorepo');8fastCheckMonorepo.refreshContent();9const fastCheckMonorepo = require('fast-check-monorepo');10fastCheckMonorepo.refreshContent();11const fastCheckMonorepo = require('fast-check-monorepo');12fastCheckMonorepo.refreshContent();13const fastCheckMonorepo = require('fast-check-monorepo');14fastCheckMonorepo.refreshContent();15const fastCheckMonorepo = require('fast-check-monorepo');16fastCheckMonorepo.refreshContent();17const fastCheckMonorepo = require('fast-check-monorepo');18fastCheckMonorepo.refreshContent();19const fastCheckMonorepo = require('fast-check-monorepo');20fastCheckMonorepo.refreshContent();21const fastCheckMonorepo = require('fast-check-monorepo');22fastCheckMonorepo.refreshContent();23const fastCheckMonorepo = require('fast-check-monorepo
Using AI Code Generation
1const { refreshContent } = require('fast-check-monorepo');2const content = refreshContent();3console.log(content);4const { refreshContent } = require('fast-check-monorepo');5const content = refreshContent();6console.log(content);7const { refreshContent } = require('fast-check-monorepo');8const content = refreshContent();9console.log(content);10const { refreshContent } = require('fast-check-monorepo');11const content = refreshContent();12console.log(content);13const { refreshContent } = require('fast-check-monorepo');14const content = refreshContent();15console.log(content);16const { refreshContent } = require('fast-check-monorepo');17const content = refreshContent();18console.log(content);19const { refreshContent } = require('fast-check-monorepo');20const content = refreshContent();21console.log(content);22const { refreshContent } = require('fast-check-monorepo');23const content = refreshContent();24console.log(content);25const { refreshContent } = require('fast-check-monorepo');26const content = refreshContent();27console.log(content);28const { refreshContent } = require('fast-check-monorepo');29const content = refreshContent();30console.log(content);31const { refreshContent } = require('fast-check-monorepo');32const content = refreshContent();33console.log(content);34const { refresh
Using AI Code Generation
1const fc = require('fast-check');2const assert = require('assert');3const { Runner } = require('fast-check');4const runner = new Runner();5const property = fc.property(fc.integer(), (i) => i > 0);6runner.run(property, { numRuns: 1 }).then(() => {7 console.log('ran once');8 runner.refreshContent();9 runner.run(property, { numRuns: 1 }).then(() => {10 console.log('ran twice');11 runner.refreshContent();12 runner.run(property, { numRuns: 1 }).then(() => {13 console.log('ran thrice');14 });15 });16});17const fc = require('fast-check');18const assert = require('assert');19const { Runner } = require('fast-check');20const runner = new Runner();21const property = fc.property(fc.integer(), (i) => i > 0);22runner.run(property, { numRuns: 1 }).then(() => {23 console.log('ran once');24 runner.refreshContent();25 runner.run(property, { numRuns: 1 }).then(() => {26 console.log('ran twice');27 runner.refreshContent();28 runner.run(property, { numRuns: 1 }).then(() => {29 console.log('ran thrice');30 runner.refreshContent();31 });32 });33});34const fc = require('fast-check');35const property = fc.property(fc.integer(), (i) => i > 0);36fc.assert(property);37Ran 1 time(s)38Ran 1 time(s)
Using AI Code Generation
1const { refreshContent } = require("fast-check-monorepo");2const { check } = require("fast-check");3const { Random } = require("fast-check");4const { seed } = require("fast-check");5const fc = require("fast-check");6refreshContent();7const fc = require("fast-check");8const random = new Random(seed);9const generateRandomNumber = () => {10 return fc.integer().generate(random).value;11};12const generateRandomArray = () => {13 return fc.array(fc.integer()).generate(random).value;14};15const generateRandomString = () => {16 return fc.string().generate(random).value;17};18const generateRandomObject = () => {19 return fc.record({20 a: fc.integer(),21 b: fc.string(),22 c: fc.boolean(),23 }).generate(random).value;24};25const generateRandomArrayObject = () => {26 return fc.array(27 fc.record({28 a: fc.integer(),29 b: fc.string(),30 c: fc.boolean(),31 })32 ).generate(random).value;33};34const generateRandomArrayString = () => {35 return fc.array(fc.string()).generate(random).value;36};37const generateRandomArrayNumber = () => {38 return fc.array(fc.integer()).generate(random).value;39};40const generateRandomArrayBoolean = () => {41 return fc.array(fc.boolean()).generate(random).value;42};43const generateRandomBoolean = () => {44 return fc.boolean().generate(random).value;45};46const generateRandomArrayArray = () => {47 return fc.array(fc.array(fc.integer())).generate(random).value;48};49const generateRandomArrayArrayString = () => {50 return fc.array(fc.array(fc.string())).generate(random).value;51};52const generateRandomArrayArrayNumber = () => {53 return fc.array(fc.array(fc.integer())).generate(random).value;54};55const generateRandomArrayArrayBoolean = () => {56 return fc.array(fc.array(fc.boolean())).generate(random).value;57};58const generateRandomArrayArrayObject = () => {59 return fc.array(60 fc.array(61 fc.record({62 a: fc.integer(),63 b: fc.string(),64 c: fc.boolean(),65 })66 ).generate(random).value;67};68const generateRandomArrayArrayArray = () => {69 return fc.array(fc.array(fc.array(fc.integer()))).generate(random).value;70};
Using AI Code Generation
1const fastCheckMonorepo = require('fast-check-monorepo');2module.exports = function refreshContent() {3 return fastCheckMonorepo.refreshContent();4};5const refreshContent = require('./test3');6describe('refreshContent', () => {7 it('should return a string', () => {8 expect(typeof refreshContent()).toBe('string');9 });10});11const fastCheckMonorepo = require('fast-check-monorepo');12module.exports = function refreshContent() {13 return fastCheckMonorepo.refreshContent();14};15const refreshContent = require('./test4');16describe('refreshContent', () => {17 it('should return a string', () => {18 expect(typeof refreshContent()).toBe('string');19 });20});21const fastCheckMonorepo = require('fast-check-monorepo');22module.exports = function refreshContent() {23 return fastCheckMonorepo.refreshContent();24};25const refreshContent = require('./test5');26describe('refreshContent', () => {27 it('should return a string', () => {28 expect(typeof refreshContent()).toBe('string');29 });30});31const fastCheckMonorepo = require('fast-check-monorepo');32module.exports = function refreshContent() {33 return fastCheckMonorepo.refreshContent();34};35const refreshContent = require('./test6');36describe('refreshContent', () => {37 it('should return a string', () => {38 expect(typeof refreshContent()).toBe('string');39 });40});41const fastCheckMonorepo = require('fast-check-monorepo');
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!