From ecedab2e65f7d73807bdbb7125ccd062e3135a93 Mon Sep 17 00:00:00 2001 From: Games for Girls Date: Sat, 14 Oct 2023 12:38:07 -0400 Subject: [PATCH] test: adding more unit tests (#107) --- client/index.js | 3 - client/index.test.js | 138 ++++- coverage/badge.svg | 2 +- coverage/coverage-summary.json | 6 +- coverage/lcov-report/index.html | 54 +- coverage/lcov.info | 880 ++++++++++++++++---------------- 6 files changed, 605 insertions(+), 478 deletions(-) diff --git a/client/index.js b/client/index.js index c6b5d977..b96cae8f 100644 --- a/client/index.js +++ b/client/index.js @@ -270,9 +270,6 @@ export default class Client { this.window.scrollTop(); // why do we do this? this.window.resize(this.zoom()); - log.debug('Client - loading client config'); - this.config = config; - // load the character this.loadCharacter(); diff --git a/client/index.test.js b/client/index.test.js index 73deddcf..b4c3fb7b 100644 --- a/client/index.test.js +++ b/client/index.test.js @@ -8,12 +8,144 @@ jest.mock('./utils/detect'); * @test {Client} */ describe('Client', () => { - const instance = new Client(); + + let instance; + + beforeEach(() => { + instance = new Client(); + }); /** * @test {Client#constructor} */ - it('.constructor()', () => { - expect(Client).toBeDefined(); + test('.constructor()', () => { + expect(instance).toBeDefined(); + }); + + /** + * @test {Client#loadClient} + */ + test('.loadClient()', () => { + const spy = jest.spyOn(instance, "loadCharacter"); + expect(spy.mock.calls.length).toEqual(0); + + instance.loadClient(); + + expect(instance.keydownEventListener).toBeDefined(); + expect(spy.mock.calls.length).toEqual(1); + }); + + /** + * @test {Client#welcomeContinue} + */ + describe('.welcomeContinue()', () => { + + it('returns false if no game', () => { + // returns false if no game + expect(instance.game).toEqual(null); + expect(instance.welcomeContinue()).toBe(false); + }); + + it('dismisses the welcome screen', () => { + // mock the body + instance.body = { + removeClass: jest.fn() + } + + // mock the game + instance.game = { + storage: { + data: { + welcome: true + }, + save: jest.fn() + } + }; + + expect(instance.game.storage.data.welcome).toEqual(true); + const welcomeScreenDismissed = instance.welcomeContinue(); + expect(instance.game.storage.data.welcome).toEqual(false); + expect(instance.game.storage.save).toHaveBeenCalled(); + expect(instance.body.removeClass).toHaveBeenCalledWith('welcomeMessage'); + expect(welcomeScreenDismissed).toEqual(true); + }); + }); + + describe('.login()', () => { + it('returns false if currently logging in', () => { + instance.loggingIn = true; + expect(instance.login()).toEqual(false); + }); + + it('returns false if no game', () => { + instance.game = null; + expect(instance.login()).toEqual(false); + }); + + it('returns false if game is not loaded', () => { + instance.game = { loaded: false }; + expect(instance.login()).toEqual(false); + }); + + it('returns false if there is a statusMessage', () => { + instance.game = { loaded: true }; + instance.statusMessage = 'something'; + expect(instance.login()).toEqual(false); + }); + + it('returns false if cannot verify login form', () => { + instance.game = { loaded: true }; + instance.statusMessage = 'something'; + instance.verifyForm = () => { return false; } + expect(instance.login()).toEqual(false); + }); + + it('returns true and connects to the game', () => { + instance.loggingIn = false; + instance.game = { loaded: true, connect: jest.fn() }; + instance.statusMessage = null; + instance.verifyForm = () => { return true; } + expect(instance.login()).toEqual(true); + expect(instance.game.connect).toHaveBeenCalled(); + }); + }); + + describe('.loginAsGuest()', () => { + it ('returns false if no game', () => { + expect(instance.loginAsGuest()).toEqual(false); + }); + + it ('returns true if game exists', () => { + instance.login = jest.fn(); + instance.game = {}; + expect(instance.guestLogin).toEqual(false); + expect(instance.loginAsGuest()).toEqual(true); + expect(instance.guestLogin).toEqual(true); + expect(instance.login).toHaveBeenCalled(); + }); + }); + + test('.loadCharacter()', () => { + const validClasses = ['about', 'git', 'credits']; + const invalidClasses = ['blah', 'wrong', '']; + + instance.game = { started: true }; + + instance.wrapper = { + hasClass: (className) => instance.wrapper.className.indexOf(className) !== -1, + attr: jest.fn().mockReturnValue(instance.wrapper.className), + removeClass: jest.fn().mockReturnValue(instance.wrapper), + addClass: jest.fn() + } + + validClasses.forEach(className => { + instance.wrapper.className = className; + expect(instance.loadCharacter()).toEqual(true); + }); + + invalidClasses.forEach(className => { + instance.wrapper.className = className; + expect(instance.loadCharacter()).toEqual(false); + }); }); }); \ No newline at end of file diff --git a/coverage/badge.svg b/coverage/badge.svg index cc51d709..44bd9eeb 100644 --- a/coverage/badge.svg +++ b/coverage/badge.svg @@ -1 +1 @@ -Coverage: 16.18%Coverage16.18% \ No newline at end of file +Coverage: 39.46%Coverage39.46% \ No newline at end of file diff --git a/coverage/coverage-summary.json b/coverage/coverage-summary.json index 36f57344..fe0901d6 100644 --- a/coverage/coverage-summary.json +++ b/coverage/coverage-summary.json @@ -1,5 +1,5 @@ -{"total": {"lines":{"total":363,"covered":60,"skipped":0,"pct":16.52},"statements":{"total":377,"covered":61,"skipped":0,"pct":16.18},"functions":{"total":70,"covered":6,"skipped":0,"pct":8.57},"branches":{"total":177,"covered":15,"skipped":0,"pct":8.47},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}} -,"/Applications/MAMP/htdocs/WTF-Adventure/client/index.js": {"lines":{"total":300,"covered":37,"skipped":0,"pct":12.33},"functions":{"total":59,"covered":1,"skipped":0,"pct":1.69},"statements":{"total":313,"covered":37,"skipped":0,"pct":11.82},"branches":{"total":121,"covered":1,"skipped":0,"pct":0.82}} -,"/Applications/MAMP/htdocs/WTF-Adventure/client/lib/log.js": {"lines":{"total":56,"covered":22,"skipped":0,"pct":39.28},"functions":{"total":10,"covered":5,"skipped":0,"pct":50},"statements":{"total":57,"covered":23,"skipped":0,"pct":40.35},"branches":{"total":51,"covered":14,"skipped":0,"pct":27.45}} +{"total": {"lines":{"total":361,"covered":146,"skipped":0,"pct":40.44},"statements":{"total":375,"covered":148,"skipped":0,"pct":39.46},"functions":{"total":70,"covered":18,"skipped":0,"pct":25.71},"branches":{"total":177,"covered":43,"skipped":0,"pct":24.29},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}} +,"/Applications/MAMP/htdocs/WTF-Adventure/client/index.js": {"lines":{"total":298,"covered":121,"skipped":0,"pct":40.6},"functions":{"total":59,"covered":13,"skipped":0,"pct":22.03},"statements":{"total":311,"covered":122,"skipped":0,"pct":39.22},"branches":{"total":121,"covered":26,"skipped":0,"pct":21.48}} +,"/Applications/MAMP/htdocs/WTF-Adventure/client/lib/log.js": {"lines":{"total":56,"covered":24,"skipped":0,"pct":42.85},"functions":{"total":10,"covered":5,"skipped":0,"pct":50},"statements":{"total":57,"covered":25,"skipped":0,"pct":43.85},"branches":{"total":51,"covered":17,"skipped":0,"pct":33.33}} ,"/Applications/MAMP/htdocs/WTF-Adventure/client/utils/modules.js": {"lines":{"total":7,"covered":1,"skipped":0,"pct":14.28},"functions":{"total":1,"covered":0,"skipped":0,"pct":0},"statements":{"total":7,"covered":1,"skipped":0,"pct":14.28},"branches":{"total":5,"covered":0,"skipped":0,"pct":0}} } diff --git a/coverage/lcov-report/index.html b/coverage/lcov-report/index.html index 825243b1..48d4c175 100644 --- a/coverage/lcov-report/index.html +++ b/coverage/lcov-report/index.html @@ -23,30 +23,30 @@

All files

- 16.18% + 39.46% Statements - 61/377 + 148/375
- 8.47% + 24.29% Branches - 15/177 + 43/177
- 8.57% + 25.71% Functions - 6/70 + 18/70
- 16.52% + 40.44% Lines - 60/363 + 146/361
@@ -80,32 +80,32 @@

All files

client - -
+ +
- 11.82% - 37/313 - 0.82% - 1/121 - 1.69% - 1/59 - 12.33% - 37/300 + 39.22% + 122/311 + 21.48% + 26/121 + 22.03% + 13/59 + 40.6% + 121/298 client/lib - -
+ +
- 40.35% - 23/57 - 27.45% - 14/51 + 43.85% + 25/57 + 33.33% + 17/51 50% 5/10 - 39.28% - 22/56 + 42.85% + 24/56 @@ -131,7 +131,7 @@

All files