Below are detailed instructions for adding tests to your Treasures and Trinkets Project. These tests are mandatory and essential for the project's completion. Failing to include these tests will result in a deduction of -1 point from your total score.
For a better understanding of how these tests work and why they are important, you can read this introductory tutorial on Jest testing.
To view the specific instructions and test code for each file, click on the respective sections below. Each section expands to reveal detailed steps and code snippets required for setting up your tests.
Adding collection.js
Tests
Follow these steps to set up and run your collection.js
tests:
-
Create a Test Folder:
- Open your Treasures and Trinkets Project on repl.it.
- If you haven't already, create a new folder in the left side panel, named
__tests__
.
-
Add the Test File:
- Inside the
__tests__
folder, create a new file namedcollection.tests.js
. - Make sure this file is placed directly within the
__tests__
folder.
- Inside the
-
Copy Test Code:
- Copy the provided test code for
collection.js
. - Paste it into your
collection.tests.js
file.
- Copy the provided test code for
-
Configure Test Settings:
- Go to the 'config files' section, located beneath 'package files' on the left side panel. (if you dont see it, you have to turn "hide hidden files off" -> this can be found if you click on the three dots next to Files)
- Open the
.replit
file.
-
Update .replit Configuration:
-
Edit the existing content in the
.replit
file to the following configuration:language="nodejs" run="echo '\nRunning collection.js Tests:\n' && jest collection.tests.js --verbose" modules = ["nodejs-20:v8-20230920-bd784b9"]
-
-
Run the Tests:
- Click the play button at the top of the repl.it interface.
- Your
collection.js
tests should now run and display in the console.
collection.tests.js code
const {
getAllCustomerEmails,
getAllCustomerFullNames,
getCountOfAllCustomerTransactions,
getUniqueListOfAllCustomerStates,
getCountOfAllProductsPurchased,
getTotalRevenueOfAllProductsSold,
getUniqueListOfAllAssociateStoreLocations,
getMostExpensiveTreasureBox
} = require('../src/collection.js');
const customerData = require('../data/sampleData.js');
describe('Testing Collection.js Functions', () => {
describe('getAllCustomerEmails', () => {
it('returns an array of all customer emails', () => {
const result = getAllCustomerEmails(customerData);
expect(result).toBeInstanceOf(Array);
});
it('returns 10 customers', () => {
const result = getAllCustomerEmails(customerData);
expect(result).toHaveLength(10);
});
it('returns specific customers', () => {
const result = getAllCustomerEmails(customerData);
const expectedEmails = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
];
expectedEmails.forEach((email, index) => {
expect(result[index]).toBe(email);
});
})
});
describe('getAllCustomerFullNames', () => {
it('returns an array of all customer full names', () => {
const result = getAllCustomerFullNames(customerData);
expect(result).toBeInstanceOf(Array);
});
it('returns the correct number of customer full names', () => {
const result = getAllCustomerFullNames(customerData);
expect(result).toHaveLength(10); // Assuming there are 10 customers in the sample data
});
it('returns correctly formatted full names', () => {
const result = getAllCustomerFullNames(customerData);
expect(result).toEqual([
'Kelley Stein',
'Tricia Hubbard',
'Mona Cain',
'Holman Jenkins',
'Lottie Alston',
'Jill Barnes',
'Lauri Castaneda',
'Jo Klein',
'Nola Murray',
'Welch Mcmillan'
]);
});
});
describe('getCountOfAllCustomerTransactions', () => {
it('returns a number of all customer transactions', () => {
const result = getCountOfAllCustomerTransactions(customerData);
expect(typeof result).toBe('number');
});
it('returns the correct total number of transactions', () => {
const result = getCountOfAllCustomerTransactions(customerData);
let expectedTransactionCount = 0;
for (const customer of customerData) {
expectedTransactionCount += customer.transactions.length;
}
expect(result).toBe(expectedTransactionCount);
});
});
describe('getUniqueListOfAllCustomerStates', () => {
it('returns a unique list of all customer states', () => {
const result = getUniqueListOfAllCustomerStates(customerData);
expect(result).toBeInstanceOf(Array);
});
it('returns exact length of unique customer states', () => {
const result = getUniqueListOfAllCustomerStates(customerData);
const uniqueStates = new Set(result);
expect(uniqueStates.size).toBe(9);
});
it('returns expected states from the data set', () => {
const result = getUniqueListOfAllCustomerStates(customerData);
const expectedStates = ['Texas', 'Louisiana', 'Missouri', 'California', 'Georgia', 'Maryland', 'Florida', 'Northern Mariana Islands', 'Wyoming'].sort();
expect(result.sort()).toEqual(expectedStates);
});
});
describe('getCountOfAllProductsPurchased', () => {
it('returns a count of all products purchased', () => {
const result = getCountOfAllProductsPurchased(customerData);
expect(typeof result).toBe('number');
let expectedCount = 0;
customerData.forEach(customer => {
customer.transactions.forEach(transaction => {
expectedCount += transaction.products.length;
});
});
expect(result).toBe(expectedCount);
});
});
describe('getTotalRevenueOfAllProductsSold', () => {
it('returns a number as the total revenue', () => {
const result = getTotalRevenueOfAllProductsSold(customerData);
expect(typeof result).toBe('number');
});
it('returns the correct total revenue of all products sold', () => {
const result = getTotalRevenueOfAllProductsSold(customerData);
let expectedRevenue = 0;
customerData.forEach(customer => {
customer.transactions.forEach(transaction => {
transaction.products.forEach(product => {
expectedRevenue += product.priceInCents;
});
});
});
expect(result).toBe(expectedRevenue);
});
});
describe('getUniqueListOfAllAssociateStoreLocations', () => {
it('returns an array as a result', () => {
const result = getUniqueListOfAllAssociateStoreLocations(customerData);
expect(result).toBeInstanceOf(Array);
});
it('returns a list with unique store locations', () => {
const result = getUniqueListOfAllAssociateStoreLocations(customerData);
const uniqueLocations = new Set(result);
expect(uniqueLocations.size).toBe(result.length);
});
});
describe('getMostExpensiveTreasureBox', () => {
it('returns an object representing a product', () => {
const result = getMostExpensiveTreasureBox(customerData);
expect(result).toBeDefined();
expect(typeof result).toBe('object');
});
it('returns a product with the highest price in the dataset', () => {
const result = getMostExpensiveTreasureBox(customerData);
let highestPrice = 0;
customerData.forEach(customer => {
customer.transactions.forEach(transaction => {
transaction.products.forEach(product => {
if (product.priceInCents > highestPrice) {
highestPrice = product.priceInCents;
}
});
});
});
expect(result.priceInCents).toBe(highestPrice);
});
});
});
Adding find.js
Tests
To properly set up and execute tests for your find.js
functions in the Treasures and Trinkets Project, follow these steps:
-
Prepare the Test Folder:
- Access your Treasures and Trinkets Project on repl.it.
- Create a new folder in the left side panel, named
__tests__
, if it's not already there.
-
Add the Test File:
- In the
__tests__
folder, create a new file namedfind.tests.js
. - Make sure this file is located directly within the
__tests__
folder.
- In the
-
Copy Test Code:
- Copy the test code specifically designed for
find.js
. - Paste it into your
find.tests.js
file.
- Copy the test code specifically designed for
-
Configure Test Environment:
- Go to the 'config files' section, located under 'package files' on the left side panel (if you dont see it, you have to turn "hide hidden files off" -> this can be found if you click on the three dots next to Files).
- Open the
.replit
file.
-
Update .replit File:
-
Edit the content in the
.replit
file to the following configuration:language="nodejs" run="echo '\nRunning find.js Tests:\n' && jest find.tests.js --verbose" modules = ["nodejs-20:v8-20230920-bd784b9"]
-
-
Execute the Tests:
- Click the play button located at the top of the repl.it interface.
- Watch as the
find.js
tests are run and their results displayed in the console.
find.tests.js code
const {
findCustomerById,
findCustomerByFullName,
findAssociateById,
findAllCustomersByState,
findAllCustomersHelpedByAssociate,
findAllCustomersWhoBoughtSpecificBox
} = require('../src/find.js');
const customerData = require('../data/sampleData.js');
describe('Testing find.js Functions', () => {
describe('findCustomerById', () => {
it('returns the customer object for a valid ID', () => {
const validCustomerId = "64136ea9a82d5ad053f1e927";
const result = findCustomerById(customerData, validCustomerId);
expect(result).toBeDefined();
expect(result.id).toBe(validCustomerId);
});
it('returns null for an invalid ID', () => {
const invalidCustomerId = 'invalidCustomerId';
const result = findCustomerById(customerData, invalidCustomerId);
expect(result).toBeNull();
});
});
describe('findCustomerByFullName', () => {
it('returns the customer object for a valid full name', () => {
// Use a valid full name from your data
const validFullName = "Kelley Stein";
const result = findCustomerByFullName(customerData, validFullName);
expect(result).toBeDefined();
expect(result.profile.name.first + " " + result.profile.name.last).toBe(validFullName);
});
it('returns null for an invalid full name', () => {
const invalidFullName = 'Invalid FullName';
const result = findCustomerByFullName(customerData, invalidFullName);
expect(result).toBeNull();
});
});
describe('findAssociateById', () => {
it('returns the associate object for a valid ID', () => {
const validAssociateId = "6413682d3baea7ee39e1b3b4"; // Use a valid associate ID from your data
const result = findAssociateById(customerData, validAssociateId);
expect(result).toBeDefined();
expect(result.id).toBe(validAssociateId);
});
it('returns null for an invalid ID', () => {
const invalidAssociateId = 'invalidAssociateId';
const result = findAssociateById(customerData, invalidAssociateId);
expect(result).toBeNull();
});
});
describe('findAllCustomersByState', () => {
it('returns all customer objects for a given state', () => {
const stateName = "Texas"; // Use a state from your data
const result = findAllCustomersByState(customerData, stateName);
expect(result).toBeInstanceOf(Array);
// Check if all returned customers are from the specified state
result.forEach(customer => {
expect(customer.profile.address.state).toBe(stateName);
});
});
it('returns an empty array for a state with no customers', () => {
const stateName = 'NoCustomerState';
const result = findAllCustomersByState(customerData, stateName);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(0);
});
});
describe('findAllCustomersHelpedByAssociate', () => {
it('returns all customer objects helped by a specific associate', () => {
const validAssociateId = "6413682d3baea7ee39e1b3b4"; // Use a valid associate ID from your data
const result = findAllCustomersHelpedByAssociate(customerData, validAssociateId);
expect(result).toBeInstanceOf(Array);
// Check if all returned customers were helped by the specified associate
result.forEach(customer => {
let helpedByAssociate = customer.transactions.some(transaction =>
transaction.associate && transaction.associate.id === validAssociateId);
expect(helpedByAssociate).toBeTruthy();
});
});
it('returns an empty array if no customers were helped by the associate', () => {
const invalidAssociateId = 'invalidAssociateId';
const result = findAllCustomersHelpedByAssociate(customerData, invalidAssociateId);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(0);
});
});
describe('findAllCustomersWhoBoughtSpecificBox', () => {
it('returns all customer objects who bought a specific product', () => {
const boxName = "Happy Birthday #2"; // Use a valid product name from your data
const result = findAllCustomersWhoBoughtSpecificBox(customerData, boxName);
expect(result).toBeInstanceOf(Array);
// Check if all returned customers bought the specified product
result.forEach(customer => {
let boughtProduct = customer.transactions.some(transaction =>
transaction.products.some(product => product.productName === boxName));
expect(boughtProduct).toBeTruthy();
});
});
it('returns an empty array if no customers bought the product', () => {
const nonexistentBoxName = 'Nonexistent Box';
const result = findAllCustomersWhoBoughtSpecificBox(customerData, nonexistentBoxName);
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(0);
});
});
});
Adding transform.js
Tests
To integrate and execute tests for your transform.js
functions, follow these steps:
-
Prepare the Test Folder:
- Access your Treasures and Trinkets Project on repl.it.
- If not already done, create a new folder named
__tests__
in the left side panel. (you only need 1__tests__
folder)
-
Create the Test File:
- In the
__tests__
folder, add a new file namedtransform.tests.js
. - Ensure this file is placed inside the
__tests__
folder.
- In the
-
Insert Test Code:
- Copy the provided test code specific to
transform.js
. - Paste this code into your
transform.tests.js
file.
- Copy the provided test code specific to
-
Set Up Test Configuration:
- Navigate to 'config files', found below 'package files' on the left side panel (if you dont see it, you have to turn "hide hidden files off" -> this can be found if you click on the three dots next to Files).
- Open the
.replit
file.
-
Modify .replit Configuration:
-
Change the existing content in the
.replit
file to:language="nodejs" run="echo '\nRunning transform.js Tests:\n' && jest transform.tests.js --verbose" modules = ["nodejs-20:v8-20230920-bd784b9"]
-
-
Execute the Tests:
- Click the play button at the top of the repl.it interface.
- Observe as your
transform.js
tests run and their results are displayed in the console.
transform.tests.js code
const { transformToAssociateByIdAndSaleTotal } = require('../src/transform.js');
const customerData = require('../data/sampleData.js');
describe('Testing transform.js Functions', () => {
describe('transformToAssociateByIdAndSaleTotal', () => {
it('returns an object with associate ids and total sale amounts', () => {
const result = transformToAssociateByIdAndSaleTotal(customerData);
expect(typeof result).toBe('object');
for (const key in result) {
expect(typeof key).toBe('string');
expect(key).not.toBe('');
expect(typeof result[key]).toBe('number');
}
});
});
});
Run All Three Tests
language="nodejs"
run="echo '\nRunning find.js Tests:\n' && jest find.tests.js --verbose && echo '\nRunning collection.js Tests:\n' && jest collection.tests.js --verbose && echo '\nRunning transform.js Tests:\n' && jest transform.tests.js --verbose"
modules = ["nodejs-20:v8-20230920-bd784b9"]