Best JavaScript code snippet using best
metrics.test.js
Source:metrics.test.js
...181 t.is(latest.id, t.context.snapshot.id);182 t.is(latest.userId, t.context.user.id);183 await snapshot.destroy();184});185test.serial('fetchComparison() returns comparison of week apart snapshots', async t => {186 const STAR_DIFFERENCE = 3;187 const FORK_DIFFERENCE = 10;188 const VIEW_DIFFERENCE = 6;189 const CLONE_DIFFERENCE = 2;190 const [snapshot] = await userSnapshots(t.context.user.id);191 const alteredSnapshot = _.cloneDeep(snapshot);192 alteredSnapshot.metrics.map(repo => {193 repo.stars -= STAR_DIFFERENCE;194 repo.forks -= FORK_DIFFERENCE;195 repo.views -= VIEW_DIFFERENCE;196 repo.clones -= CLONE_DIFFERENCE;197 return repo;198 });199 const previousSnapshot = await createSnapshot(200 {201 metrics: alteredSnapshot.metrics,202 userId: t.context.user.id,203 createdAt: moment().subtract(WEEK_IN_DAYS, 'days'),204 },205 {206 returning: true,207 },208 );209 const comparison = await fetchComparison(t.context.user.id);210 const expectedRepoNames = REPOS211 .filter(repo => repo.selected)212 .map(repo => repo.name);213 const actualRepoNames = comparison.map(repo => repo.name);214 t.deepEqual(actualRepoNames, expectedRepoNames);215 t.true(comparison.length > 0);216 comparison.forEach(repo => t.deepEqual(Object.keys(repo), REPO_KEYS));217 comparison.forEach(repo => {218 t.deepEqual(Object.keys(repo), REPO_KEYS);219 Object.keys(repo).filter(key => key !== 'name').forEach(key => {220 t.deepEqual(Object.keys(repo[key]), COMPARISON_KEYS);221 });222 });223 t.true(comparison.every(repo => repo.stars.difference === STAR_DIFFERENCE));224 t.true(comparison.every(repo => repo.forks.difference === FORK_DIFFERENCE));225 t.true(comparison.every(repo => repo.views.difference === VIEW_DIFFERENCE));226 t.true(comparison.every(repo => repo.clones.difference === CLONE_DIFFERENCE));227 await previousSnapshot.destroy();228});229test.serial('fetchComparison() returns comparison based on selected metric types', async t => {230 const [snapshot] = await userSnapshots(t.context.user.id);231 const previousSnapshot = await createSnapshot(232 {233 metrics: snapshot.metrics,234 userId: t.context.user.id,235 createdAt: moment().subtract(WEEK_IN_DAYS, 'days'),236 },237 {238 returning: true,239 },240 );241 const UNSELECTED_METRIC_TYPES = new Set(['views', 'forks']);242 const ALTERED_METRIC_TYPES = _.cloneDeep(METRIC_TYPES).map(metricType => {243 if (UNSELECTED_METRIC_TYPES.has(metricType.name)) {244 metricType.selected = false;245 }246 return metricType;247 });248 await setUserMetricTypes(t.context.user, ALTERED_METRIC_TYPES);249 const comparison = await fetchComparison(t.context.user.id);250 t.true(comparison.length > 0);251 const actualRepoNames = comparison.map(repo => repo.name);252 const expectedRepoNames = REPOS253 .filter(repo => repo.selected)254 .map(repo => repo.name);255 t.deepEqual(actualRepoNames, expectedRepoNames);256 const EXPECTED_REPO_KEYS = REPO_KEYS.filter(key => !UNSELECTED_METRIC_TYPES.has(key));257 comparison.forEach(repo => t.deepEqual(Object.keys(repo), EXPECTED_REPO_KEYS));258 comparison.forEach(repo => {259 t.deepEqual(Object.keys(repo), EXPECTED_REPO_KEYS);260 Object.keys(repo).filter(key => key !== 'name').forEach(key => {261 t.deepEqual(Object.keys(repo[key]), COMPARISON_KEYS);262 });263 });264 await previousSnapshot.destroy();265});266test.serial('fetchComparison() returns null without sufficient snapshots', async t => {267 const comparison = await fetchComparison(t.context.user.id);268 t.is(comparison, null);...
queues.test.js
Source:queues.test.js
...34 sinon.assert.calledWith(ingest, user.id);35 t.deepEqual(jobResult, SNAPSHOT);36 ingest.restore();37});38test.serial('sendEmailWorker calls metrics.fetchComparison() and email.send()', async t => {39 const fetchComparison = sinon.stub(metrics, 'fetchComparison');40 fetchComparison.returns(Promise.resolve(WEEKLY_METRICS));41 const send = sinon.stub(email, 'send');42 send.returns(Promise.resolve(SENDGRID_SUCCESS));43 const jobResult = await sendEmailWorker(job);44 t.true(fetchComparison.calledOnce);45 t.true(send.calledOnce);46 sinon.assert.calledWith(fetchComparison, user.id);47 sinon.assert.calledWith(send, {48 user,49 metrics: WEEKLY_METRICS,50 date: moment().format('LL'),51 });52 t.deepEqual(jobResult, SENDGRID_SUCCESS);...
ComparisonResult.js
Source:ComparisonResult.js
...30 if (this.timer) {31 clearTimeout(this.timer);32 }33 }34 fetchComparison() {35 this.props.getComparison(this.props.research.comparisonID);36 }37 formatTime(time) {38 if (time > 9) {39 return _.toString(time);40 }41 return `0${time}`;42 }43 renderProgress() {44 const {comparison} = this.props;45 if (comparison.finished || !comparison.total) {46 return null;47 }48 const percentage = Math.min(100 * _.round(comparison.processed / comparison.total, 2), 100);...
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!!