This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from 30-seconds/add-function-tests
Add tests for utilities and search engine
- Loading branch information
Showing
7 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import searchIndexingEngine from './searchIndexingEngine'; | ||
|
||
describe('searchIndexingEngine', () => { | ||
it('parses the tokens from the given string', () => { | ||
const str = 'Creates an object with the same values as the provided object and keys generated by running the provided function for each key. Use Object.keys(obj) to iterate over the object\'s keys. Use Array.prototype.reduce() to create a new object with the same values and mapped keys using fn. Additionally, yeet caress seed recreational practicality'; | ||
const result = ['creat', 'object', 'valu', 'provid', 'kei', 'gener', 'run', 'function', 'us', 'obj', 'iter', "object'", 'arrai', 'prototyp', 'reduc', 'new', 'map', 'fn', 'addition', 'yeet', 'caress', 'seed', 'recreat', 'practic']; | ||
expect(searchIndexingEngine(str)).toEqual(result); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
determineExpertiseFromTags, | ||
stripExpertiseFromTags, | ||
transformSnippetIndex, | ||
uniqueElements, | ||
similarity, | ||
weightedSample, | ||
chunk | ||
} from './array'; | ||
|
||
describe('determineExpertiseFromTags', () => { | ||
it('determines expertise from tags', () => { | ||
expect(determineExpertiseFromTags(['array', 'advanced'])).toBe('advanced'); | ||
}); | ||
it('returns default expertise if none is found', () => { | ||
expect(determineExpertiseFromTags(['array'])).toBe('intermediate'); | ||
}); | ||
}); | ||
|
||
describe('stripExpertiseFromTags', () => { | ||
it('strips expertise from tags', () => { | ||
expect(stripExpertiseFromTags(['array', 'advanced'])).toEqual(['array']); | ||
}); | ||
}); | ||
|
||
describe('transformSnippetIndex', () => { | ||
it('transforms the snippet index', () => { | ||
const edges = [ | ||
{ | ||
node: { | ||
title: 'a', | ||
expertise: 'intermediate', | ||
tags: { | ||
primary: 'array', | ||
}, | ||
language: { | ||
long: 'lang', | ||
short: 'l', | ||
}, | ||
html: { | ||
description: 'desc ', | ||
}, | ||
slug: '/a', | ||
searchTokens: '', | ||
irrelevantStuff: 'data', | ||
}, | ||
}, | ||
]; | ||
const result = transformSnippetIndex(edges); | ||
expect(result[0].title).toBe(edges[0].node.title); | ||
expect(result[0].expertise).toBe(edges[0].node.expertise); | ||
expect(result[0].primaryTag).toBe(edges[0].node.tags.primary); | ||
expect(result[0].language).toBe(edges[0].node.language.long); | ||
expect(result[0].description).toBe(edges[0].node.html.description.trim()); | ||
expect(result[0].url).toBe(edges[0].node.slug); | ||
expect(result[0].searchTokens).toBe(edges[0].node.searchTokens); | ||
expect(result[0].irrelevantStuff).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('uniqueElements', () => { | ||
it('returns the unique elements in an array', () => { | ||
expect(uniqueElements([1, 2, 2, 3, 4, 4, 5])).toEqual([1, 2, 3, 4, 5]); | ||
}); | ||
}); | ||
|
||
describe('similarity', () => { | ||
it('returns an array of elements that appear in both arrays.', () => { | ||
expect(similarity([1, 2, 3], [1, 2, 4])).toEqual([1, 2]); | ||
}); | ||
}); | ||
|
||
describe('weightedSample', () => { | ||
it('returns a random element from the array', () => { | ||
const arr = [3, 7, 9, 11]; | ||
const weights = [0.1, 0.2, 0.6, 0.1]; | ||
expect(arr.includes(weightedSample(arr, weights))).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
describe('chunk', () => { | ||
it('chunks an array with a remainder', () => { | ||
expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]); | ||
}); | ||
it('chunks an empty array', () => { | ||
expect(chunk([])).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { | ||
mapNumRange | ||
} from './math'; | ||
|
||
describe('mapNumRange', () => { | ||
it('maps 5 to the range 0-100 from 0-10', () => { | ||
expect(mapNumRange(5, 0, 10, 0, 100)).toEqual(50); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters