I have a function which selects a random value from an array to be returned:
<code>export function selectMovieUnderDuration(options: IStreamRequest, movies: Movie[], prevMovies: Movie[], duration: number): Movie {
let filteredMovies: Movie[] = movies.filter(movie =>
movie.Tags.some(tag => options.Tags.includes(tag)) &&
movie.DurationLimit <= duration
let notRepeatMovies: Movie[] = filteredMovies.filter((item) => !prevMovies.some((obj) => obj.LoadTitle === item.LoadTitle));
let selectedMovie: Movie = notRepeatMovies.length > 0 ?
notRepeatMovies[Math.floor(Math.random() * notRepeatMovies.length)] :
filteredMovies[Math.floor(Math.random() * filteredMovies.length)];
<code>export function selectMovieUnderDuration(options: IStreamRequest, movies: Movie[], prevMovies: Movie[], duration: number): Movie {
let filteredMovies: Movie[] = movies.filter(movie =>
movie.Tags.some(tag => options.Tags.includes(tag)) &&
movie.DurationLimit <= duration
);
let notRepeatMovies: Movie[] = filteredMovies.filter((item) => !prevMovies.some((obj) => obj.LoadTitle === item.LoadTitle));
let selectedMovie: Movie = notRepeatMovies.length > 0 ?
notRepeatMovies[Math.floor(Math.random() * notRepeatMovies.length)] :
filteredMovies[Math.floor(Math.random() * filteredMovies.length)];
return selectedMovie;
}
</code>
export function selectMovieUnderDuration(options: IStreamRequest, movies: Movie[], prevMovies: Movie[], duration: number): Movie {
let filteredMovies: Movie[] = movies.filter(movie =>
movie.Tags.some(tag => options.Tags.includes(tag)) &&
movie.DurationLimit <= duration
);
let notRepeatMovies: Movie[] = filteredMovies.filter((item) => !prevMovies.some((obj) => obj.LoadTitle === item.LoadTitle));
let selectedMovie: Movie = notRepeatMovies.length > 0 ?
notRepeatMovies[Math.floor(Math.random() * notRepeatMovies.length)] :
filteredMovies[Math.floor(Math.random() * filteredMovies.length)];
return selectedMovie;
}
I know this works correctly after many live tests, but I would like to write some automated tests for this in mocha/chai.
I already have a couple tests that cover basic functionality:
<code>describe('selectMovieUnderDuration', () => {
it('should return a movie that is under the duration limit that has the tags in the request object', () => {
let movies: Movie[] = [inception, matrix, interstellar, dune];
let prevMovies: Movie[] = [];
let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi']);
let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 9000);
expect(result).to.be.equal(inception);
it('should return a movie that is under the duration limit that has the tags in the request object and has not been selected before', () => {
let movies: Movie[] = [inception, matrix, interstellar, dune];
let prevMovies: Movie[] = [inception, matrix, dune];
let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi', 'Action']);
let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 10800);
expect(result).to.be.equal(interstellar);
<code>describe('selectMovieUnderDuration', () => {
it('should return a movie that is under the duration limit that has the tags in the request object', () => {
let movies: Movie[] = [inception, matrix, interstellar, dune];
let prevMovies: Movie[] = [];
let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi']);
let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 9000);
expect(result).to.be.equal(inception);
});
it('should return a movie that is under the duration limit that has the tags in the request object and has not been selected before', () => {
let movies: Movie[] = [inception, matrix, interstellar, dune];
let prevMovies: Movie[] = [inception, matrix, dune];
let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi', 'Action']);
let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 10800);
expect(result).to.be.equal(interstellar);
});
});
</code>
describe('selectMovieUnderDuration', () => {
it('should return a movie that is under the duration limit that has the tags in the request object', () => {
let movies: Movie[] = [inception, matrix, interstellar, dune];
let prevMovies: Movie[] = [];
let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi']);
let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 9000);
expect(result).to.be.equal(inception);
});
it('should return a movie that is under the duration limit that has the tags in the request object and has not been selected before', () => {
let movies: Movie[] = [inception, matrix, interstellar, dune];
let prevMovies: Movie[] = [inception, matrix, dune];
let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi', 'Action']);
let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 10800);
expect(result).to.be.equal(interstellar);
});
});
However, I now want to write at least one test where all the items available in the “movies” array are also in the “prevMovies” array, so it is forced to pick any value that would have previously been limited by the “prevMovies” array.
I know I could write something like:
<code> it('should return any movie that is under the duration limit that has the correct tags, when all movies with the criteria have been selected before', () => {
let movies: Movie[] = [inception, matrix, interstellar, dune];
let prevMovies: Movie[] = [inception, matrix, interstellar, dune];
let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi', 'Action']);
let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 9000);
expect(movies).to.be.oneOf([inception, matrix]);
<code> it('should return any movie that is under the duration limit that has the correct tags, when all movies with the criteria have been selected before', () => {
let movies: Movie[] = [inception, matrix, interstellar, dune];
let prevMovies: Movie[] = [inception, matrix, interstellar, dune];
let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi', 'Action']);
let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 9000);
expect(movies).to.be.oneOf([inception, matrix]);
});
</code>
it('should return any movie that is under the duration limit that has the correct tags, when all movies with the criteria have been selected before', () => {
let movies: Movie[] = [inception, matrix, interstellar, dune];
let prevMovies: Movie[] = [inception, matrix, interstellar, dune];
let args: ContStreamRequest = new ContStreamRequest('password', 'title', 'env', [], ['Sci-Fi', 'Action']);
let result = proEng.selectMovieUnderDuration(args, movies, prevMovies, 9000);
expect(movies).to.be.oneOf([inception, matrix]);
});
While this is great for this scenario because I know the function operates correctly, if the function did not operate correctly and I wrote this test, and sometimes the function would return dune or interstellar instead but only sometimes, the test would only fail sometimes.
Is there a way to make sure the test either consistently passes or consistently fails? I am considering that I might have to rewrite the function to break out certain parts and making them individually testable, but that seems extremely granular for how simple the function is already.