mirror of
				https://github.com/cse110-fa22-group29/cse110-fa22-group29.git
				synced 2025-10-30 19:46:49 +00:00 
			
		
		
		
	Merge pull request #38 from cse110-fa22-group29/js-unit-test-ci
Implement JS unit test framework and CI pipeline actions
This commit is contained in:
		
							
								
								
									
										2
									
								
								.github/workflows/deploy-githubpages.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/deploy-githubpages.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,5 +1,5 @@ | |||||||
| # Simple workflow for deploying static content to GitHub Pages | # Simple workflow for deploying static content to GitHub Pages | ||||||
| name: Deploy static content to Pages | name: Deploy GitHub Pages | ||||||
|  |  | ||||||
| on: | on: | ||||||
|   # Runs on pushes targeting the default branch |   # Runs on pushes targeting the default branch | ||||||
|   | |||||||
							
								
								
									
										25
									
								
								.github/workflows/js-unittest.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								.github/workflows/js-unittest.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | |||||||
|  | name: Jest JS Unit Test | ||||||
|  |  | ||||||
|  | on: | ||||||
|  |   pull_request: | ||||||
|  |     branches: | ||||||
|  |     - main | ||||||
|  |  | ||||||
|  |   # Allows you to run this workflow manually from the Actions tab | ||||||
|  |   workflow_dispatch: | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   # Single deploy job since we're just deploying | ||||||
|  |   test: | ||||||
|  |     runs-on: ubuntu-latest | ||||||
|  |     steps: | ||||||
|  |       - name: Install apt updates | ||||||
|  |         run: sudo apt -y update; sudo apt -y upgrade; | ||||||
|  |       - name: Install prerequisites | ||||||
|  |         run: sudo apt install -y nodejs npm; | ||||||
|  |       - name: Checkout | ||||||
|  |         uses: actions/checkout@v3 | ||||||
|  |       - name: Install dependencies | ||||||
|  |         run: sudo npm install | ||||||
|  |       - name: Run tests | ||||||
|  |         run: sudo npm test | ||||||
							
								
								
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | |||||||
|  | **/.devcontainer/* | ||||||
|  | **/node_modules/* | ||||||
|  | **/package-lock.json | ||||||
							
								
								
									
										10
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | { | ||||||
|  |   "name": "food-journal", | ||||||
|  |   "version": "1.0.0", | ||||||
|  |   "scripts": { | ||||||
|  |     "test": "mocha --recursive **/*.test.js" | ||||||
|  |   }, | ||||||
|  |   "dependencies": { | ||||||
|  |     "mocha": "10" | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										28
									
								
								testenv/testenv.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								testenv/testenv.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | |||||||
|  | module.exports = {environment}; | ||||||
|  |  | ||||||
|  | function environment () { | ||||||
|  | 	const localStorageMock = (function () { | ||||||
|  | 		let store = {}; | ||||||
|  | 		return { | ||||||
|  | 			getItem(key) { | ||||||
|  | 				return store[key]; | ||||||
|  | 			}, | ||||||
|  | 			setItem(key, value) { | ||||||
|  | 				store[key] = value; | ||||||
|  | 			}, | ||||||
|  | 			clear() { | ||||||
|  | 				store = {}; | ||||||
|  | 			}, | ||||||
|  | 			removeItem(key) { | ||||||
|  | 				delete store[key]; | ||||||
|  | 			}, | ||||||
|  | 			getAll() { | ||||||
|  | 				return store; | ||||||
|  | 			}, | ||||||
|  | 		}; | ||||||
|  | 	})(); | ||||||
|  |  | ||||||
|  | 	let window = {}; | ||||||
|  | 	Object.defineProperty(window, "localStorage", { value: localStorageMock }); | ||||||
|  | 	return window; | ||||||
|  | } | ||||||
							
								
								
									
										80
									
								
								testenv/testenv.test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								testenv/testenv.test.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,80 @@ | |||||||
|  | const {environment} = require("./testenv.js"); | ||||||
|  | var assert = require('assert'); | ||||||
|  | var {saveToLocal, getFromLocal, removeFromLocal, clearLocal} = require('./testenv_helpers'); | ||||||
|  |  | ||||||
|  | beforeEach(() => { | ||||||
|  | 	window = environment(); | ||||||
|  | }); | ||||||
|  |  | ||||||
|  | describe("test localStorage mock", () => { | ||||||
|  | 	it("test save and fetch", () => { | ||||||
|  | 		saveToLocal("testkey1", "testvalue1"); | ||||||
|  | 		saveToLocal("testkey2", "testvalue2"); | ||||||
|  | 		saveToLocal("testkey3", "testvalue3"); | ||||||
|  | 		saveToLocal("testkey4", "testvalue4"); | ||||||
|  |  | ||||||
|  | 		assert.equal(getFromLocal("testkey1"), "testvalue1"); | ||||||
|  | 		assert.equal(getFromLocal("testkey2"), "testvalue2"); | ||||||
|  | 		assert.equal(getFromLocal("testkey3"), "testvalue3"); | ||||||
|  | 		assert.equal(getFromLocal("testkey4"), "testvalue4"); | ||||||
|  |  | ||||||
|  | 		saveToLocal("testkey6", "testvalue5"); | ||||||
|  | 		assert.equal(getFromLocal("testkey6"), "testvalue5"); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it("test window locality", () => { | ||||||
|  | 		assert.equal(getFromLocal("testkey1"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey2"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey3"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey4"), null); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it("test delete and fetch", () => { | ||||||
|  | 		saveToLocal("testkey1", "testvalue1"); | ||||||
|  | 		saveToLocal("testkey2", "testvalue2"); | ||||||
|  | 		saveToLocal("testkey3", "testvalue3"); | ||||||
|  | 		saveToLocal("testkey4", "testvalue4"); | ||||||
|  |  | ||||||
|  | 		removeFromLocal("testkey3"); | ||||||
|  |  | ||||||
|  | 		assert.equal(getFromLocal("testkey1"), "testvalue1"); | ||||||
|  | 		assert.equal(getFromLocal("testkey2"), "testvalue2"); | ||||||
|  | 		assert.equal(getFromLocal("testkey3"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey4"), "testvalue4"); | ||||||
|  |  | ||||||
|  | 		removeFromLocal("testkey1"); | ||||||
|  |  | ||||||
|  | 		assert.equal(getFromLocal("testkey1"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey2"), "testvalue2"); | ||||||
|  | 		assert.equal(getFromLocal("testkey3"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey4"), "testvalue4"); | ||||||
|  |  | ||||||
|  | 		removeFromLocal("testkey4"); | ||||||
|  | 		 | ||||||
|  | 		assert.equal(getFromLocal("testkey1"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey2"), "testvalue2"); | ||||||
|  | 		assert.equal(getFromLocal("testkey3"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey4"), null); | ||||||
|  |  | ||||||
|  | 		removeFromLocal("testkey2"); | ||||||
|  | 		 | ||||||
|  | 		assert.equal(getFromLocal("testkey1"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey2"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey3"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey4"), null); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
|  | 	it("test clear and fetch", () => { | ||||||
|  | 		saveToLocal("testkey1", "testvalue1"); | ||||||
|  | 		saveToLocal("testkey2", "testvalue2"); | ||||||
|  | 		saveToLocal("testkey3", "testvalue3"); | ||||||
|  | 		saveToLocal("testkey4", "testvalue4"); | ||||||
|  |  | ||||||
|  | 		clearLocal(); | ||||||
|  |  | ||||||
|  | 		assert.equal(getFromLocal("testkey1"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey2"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey3"), null); | ||||||
|  | 		assert.equal(getFromLocal("testkey4"), null); | ||||||
|  | 	}); | ||||||
|  | }); | ||||||
							
								
								
									
										17
									
								
								testenv/testenv_helpers.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								testenv/testenv_helpers.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | |||||||
|  | module.exports = {saveToLocal, getFromLocal, removeFromLocal, clearLocal}; | ||||||
|  |  | ||||||
|  | function saveToLocal (k, v) { | ||||||
|  | 	window.localStorage.setItem(k, v); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function getFromLocal (k) { | ||||||
|  | 	return window.localStorage.getItem(k); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function removeFromLocal (k) { | ||||||
|  | 	window.localStorage.removeItem(k); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | function clearLocal () { | ||||||
|  | 	window.localStorage.clear(); | ||||||
|  | }  | ||||||
		Reference in New Issue
	
	Block a user