Skip to content

Commit

Permalink
v1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
CarcajadaArtificial committed Oct 22, 2024
1 parent ce48c9e commit a63ca56
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- Event throttling/debouncing.
- Key hold detection.

## v1.0.7

- Added unit tests for the `isMacOS()` function.

## v1.0.6

- Added a fix to an explict return error.
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@carcajada/teclas",
"version": "1.0.6",
"version": "1.0.7",
"exports": "./mod.ts",
"tasks": {
"dev": "deno run --watch main.ts"
Expand Down
17 changes: 10 additions & 7 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
* @returns {boolean}
* True if the current OS is macOS, false otherwise.
*/
const isMacOS = (): boolean => /Mac/.test(globalThis.navigator.userAgent);
export const isMacOS = (): boolean =>
/Macintosh/.test(globalThis.navigator.userAgent);

// =====================================================================================================
// Types
Expand All @@ -66,8 +67,8 @@ export interface Event {
* keystroke combination is detected.
*/
export interface iKeystroke {
keys: ((ev: Event) => boolean)[];
except: ((ev: Event) => boolean)[];
keys: CheckKeypress[];
except?: CheckKeypress[];
cb: (ev: Event) => void;
}

Expand Down Expand Up @@ -98,10 +99,12 @@ export const checkKeystroke = (keystroke: iKeystroke, ev: Event): boolean =>
keystroke.keys.reduce<boolean>(
(accumulator, currentValue) => accumulator && currentValue(ev),
true,
) && keystroke.except.reduce<boolean>(
(accumulator, currentValue) => accumulator && !currentValue(ev),
true,
);
) && (keystroke.except
? keystroke.except.reduce<boolean>(
(accumulator, currentValue) => accumulator && !currentValue(ev),
true,
)
: true);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
Expand Down
87 changes: 87 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// _____ _
// |_ _|__ __| |_
// | |/ -_|_-< _|
// |_|\___/__/\__|
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Welcome to the unit testing module. Some aspects of this library are strictly client-specific,
* because of these, some functionalities must be mucked up, like user agents and keyboard events.
*
* @module
*/
import { assertEquals } from 'jsr:@std/assert';
import { isMacOS } from './mod.ts';
const originalNavigator = globalThis.navigator;

// =====================================================================================================
// isMacOS();
// =====================================================================================================
const mockUserAgent = (ua: string) => {
Object.defineProperty(globalThis, 'navigator', {
value: {
userAgent: ua,
},
writable: true,
configurable: true,
});
};

const testCasesIsMacOS = [
{
name: 'returns true for Safari on macOS',
ua:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15',
expected: true,
},
{
name: 'returns true for Chrome on macOS',
ua:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
expected: true,
},
{
name: 'returns false for Windows 10 with Edge',
ua:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246',
expected: false,
},
{
name: 'returns false for Android Device',
ua:
'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36',
expected: false,
},
{
name: 'returns false for iPhone',
ua:
'Mozilla/5.0 (iPhone; CPU iPhone OS 15_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Mobile/19E241 Safari/604.1',
expected: false,
},
];

Deno.test('isMacOS function tests with sub-tests', async (t) => {
for (const testCase of testCasesIsMacOS) {
await t.step(testCase.name, () => {
// Mock the navigator.userAgent
mockUserAgent(testCase.ua);

// Execute the function
const result = isMacOS();

// Assert the expected outcome
assertEquals(
result,
testCase.expected,
`Failed for test case: ${testCase.name}`,
);

// Restore the original navigator after each test case
Object.defineProperty(globalThis, 'navigator', {
value: originalNavigator,
writable: true,
configurable: true,
});
});
}
});

0 comments on commit a63ca56

Please sign in to comment.