Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Shepherd.isTourEnabled #2686

Merged
merged 4 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions shepherd.js/src/utils/datarequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface ActorResponse {
interface TourStateDb extends DBSchema {
tours: {
key: string;
value: { id: string; isActive: boolean };
value: { isActive: boolean; uniqueId: string };
};
}

Expand Down Expand Up @@ -50,6 +50,7 @@ class DataRequest {
if (!response.ok) {
throw new Error('Could not fetch state for tours 🐑');
}

const { data } = await response.json();

this.tourStateDb = await openDB<TourStateDb>('TourState', 1, {
Expand All @@ -70,7 +71,7 @@ class DataRequest {
}
} catch (error) {
throw new Error(
'Error fetching data:' +
'Error fetching data: ' +
(error instanceof Error ? error.message : 'Unknown error')
);
}
Expand Down Expand Up @@ -101,7 +102,7 @@ class DataRequest {
return data;
} catch (error) {
throw new Error(
'Error fetching data:' +
'Error fetching data: ' +
(error instanceof Error ? error.message : 'Unknown error')
);
}
Expand Down
2 changes: 2 additions & 0 deletions test/unit/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default {
// An array of file extensions your modules use
moduleFileExtensions: ['js', 'ts', 'svelte'],

resetMocks: false,

// The root directory that Jest should scan for tests and modules within
rootDir: './',

Expand Down
4 changes: 4 additions & 0 deletions test/unit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
"@testing-library/svelte": "^4.1.0",
"autoprefixer": "^10.4.19",
"babel-jest": "^29.7.0",
"core-js": "^3.36.1",
"cross-env": "^7.0.3",
"del": "^7.1.0",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^27.9.0",
"fake-indexeddb": "^5.0.2",
"http-server": "^14.1.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-expect-message": "^1.1.3",
"jest-fetch-mock": "^3.0.3",
"jest-localstorage-mock": "^2.4.26",
"jest-transform-css": "^6.0.1",
"postcss": "^8.4.38",
"postinstall-postinstall": "^2.1.0",
Expand Down
74 changes: 63 additions & 11 deletions test/unit/pro.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { jest } from '@jest/globals';

import fetchMock from 'jest-fetch-mock';

import Shepherd from '../../shepherd.js/src/shepherd';
import DataRequest from '../../shepherd.js/src/utils/datarequest';

Expand Down Expand Up @@ -35,12 +37,26 @@ describe('Shepherd Pro', function () {
.spyOn(DataRequest.prototype, 'sendEvents')
.mockImplementation(() => Promise.resolve({ actorId: 1 }));

const getTourStateMock = jest
.spyOn(DataRequest.prototype, 'getTourState')
.mockImplementation(() => Promise.resolve([{ accountId: 1, uniqueId: 'tour-1', isActive: true }]));
beforeAll(() => {
fetchMock.enableFetchMocks();
});

beforeEach(() => {
fetch.resetMocks();

fetch.mockResponse((req) => {
if (req.url === 'https://shepherdpro.com/api/v1/state') {
return Promise.resolve(
JSON.stringify({
data: [{ uniqueId: 'tour-1', isActive: true }]
})
);
}
});
});

afterAll(() => {
sendEventsMock.mockReset();
sendEventsMock.mockRestore();
});

it('exists and creates an instance', () => {
Expand All @@ -56,11 +72,13 @@ describe('Shepherd Pro', function () {
);
});

it('sends events and passes properties and context', () => {
it('sends events and passes properties and context', async () => {
const windowSpy = jest.spyOn(global, 'window', 'get');
windowSpy.mockImplementation(() => windowProps);

Shepherd.init('api_123', 'https://api.shepherdpro.com', { extra: 'stuff' });
await Shepherd.init('api_123', 'https://shepherdpro.com', {
extra: 'stuff'
});

expect(typeof Shepherd.trigger).toBe('function');
expect(Shepherd.dataRequester.properties).toMatchObject({
Expand Down Expand Up @@ -88,20 +106,54 @@ describe('Shepherd Pro', function () {
windowSpy.mockRestore();
});

it('creates a Tour instance', () => {
it('creates a Tour instance', async () => {
const defaultStepOptions = {
classes: 'class-1 class-2'
};
Shepherd.init('api_123');
await Shepherd.init('api_123');
const tourInstance = new Shepherd.Tour({ defaultStepOptions });

expect(tourInstance instanceof Shepherd.Tour).toBe(true);
});

it('sets the userId', () => {
Shepherd.init('api_123');
it('sets the userId', async () => {
await Shepherd.init('api_123');

const userStored = window.localStorage.getItem('shepherdPro:userId');
const userStored = localStorage.getItem('shepherdPro:userId');
expect(userStored).toBe('1');
});

it('Shepherd.isTourEnabled is true when isActive is true', async () => {
const defaultStepOptions = {
classes: 'class-1 class-2'
};

await Shepherd.init('api_123');

new Shepherd.Tour({ defaultStepOptions, id: 'tour-1' });

expect(await Shepherd.isTourEnabled('tour-1')).toBe(true);
});

it('Shepherd.isTourEnabled is false when isActive is false', async () => {
fetch.mockResponseOnce((req) => {
if (req.url === 'https://shepherdpro.com/api/v1/state') {
return Promise.resolve(
JSON.stringify({
data: [{ uniqueId: 'tour-1', isActive: false }]
})
);
}
});

const defaultStepOptions = {
classes: 'class-1 class-2'
};

await Shepherd.init('api_123');

new Shepherd.Tour({ defaultStepOptions, id: 'tour-1' });

expect(await Shepherd.isTourEnabled('tour-1')).toBe(false);
});
});
3 changes: 3 additions & 0 deletions test/unit/setupTests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { jest } from '@jest/globals';
import 'core-js/stable/structured-clone';
import 'fake-indexeddb/auto';
import 'regenerator-runtime/runtime';
import 'jest-expect-message';
import 'jest-localstorage-mock';
import '@testing-library/jest-dom/extend-expect';

// Console errors are used for user information, do not display them during
Expand Down
2 changes: 1 addition & 1 deletion test/unit/utils/datarequest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ global.fetch = jest.fn();
describe('DataRequest', () => {
const defaultOptions = [
'apiKey_12345',
'https://api.shepherdpro.com',
'https://shepherdpro.com',
{ extra: 'stuff' }
];

Expand Down