• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cartesi / rollups-explorer / 10497914455

21 Aug 2024 09:40PM UTC coverage: 93.39% (-0.4%) from 93.766%
10497914455

Pull #232

github

nevendyulgerov
test(apps/web): Add unit tests
Pull Request #232: #229 Add import and export for specifications

1210 of 1432 branches covered (84.5%)

Branch coverage included in aggregate %.

486 of 569 new or added lines in 8 files covered. (85.41%)

50 existing lines in 10 files now uncovered.

12863 of 13637 relevant lines covered (94.32%)

45.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

49.32
/apps/web/src/components/specification/hooks/useSpecificationsTransfer.tsx
1
"use client";
1✔
2

1✔
3
import { ReactNode, useCallback, useMemo } from "react";
1✔
4
import { notifications } from "@mantine/notifications";
1✔
5
import { useSpecification } from "./useSpecification";
1✔
6
import {
1✔
7
    Specification,
1✔
8
    SpecificationTransfer as SpecificationTransferModel,
1✔
9
    SPECIFICATION_TRANSFER_NAME,
1✔
10
} from "../types";
1✔
11
import {
1✔
12
    validateSpecification,
1✔
13
    VALIDATOR_VERSION,
1✔
14
} from "../transfer/validator";
1✔
15

1✔
16
export const useSpecificationsTransfer = () => {
1✔
17
    const { listSpecifications, addSpecification } = useSpecification();
42✔
18
    const specifications = listSpecifications();
42✔
19
    const version = Number(VALIDATOR_VERSION);
42✔
20
    const specificationExport: SpecificationTransferModel = useMemo(
42✔
21
        () => ({
42✔
22
            name: SPECIFICATION_TRANSFER_NAME,
19✔
23
            version,
19✔
24
            timestamp: new Date().getTime(),
19✔
25
            specifications: specifications ?? [],
19✔
26
        }),
19✔
27
        [specifications, version],
42✔
28
    );
42✔
29

42✔
30
    const specificationExportLink = useMemo(
42✔
31
        () =>
42✔
32
            `data:text/json;charset=utf-8,${encodeURIComponent(
19✔
33
                JSON.stringify(specificationExport, null, 4),
19✔
34
            )}`,
19✔
35
        [specificationExport],
42✔
36
    );
42✔
37

42✔
38
    const displayAlert = useCallback(
42✔
39
        (message: ReactNode, type: "success" | "error" = "error") => {
42✔
NEW
40
            const isSuccess = type === "success";
×
NEW
41
            notifications.show({
×
NEW
42
                message,
×
NEW
43
                color: isSuccess ? "green" : "red",
×
NEW
44
                withBorder: true,
×
NEW
45
                autoClose: isSuccess,
×
NEW
46
            });
×
NEW
47
        },
×
48
        [],
42✔
49
    );
42✔
50

42✔
51
    const onValidateSuccessfully = useCallback(
42✔
52
        (specificationImport: SpecificationTransferModel) => {
42✔
NEW
53
            Promise.all(
×
NEW
54
                specificationImport.specifications.map(
×
NEW
55
                    (specification: Specification) =>
×
NEW
56
                        new Promise((resolve, reject) => {
×
NEW
57
                            setTimeout(() =>
×
NEW
58
                                addSpecification(specification, {
×
NEW
59
                                    onSuccess: () => resolve(undefined),
×
NEW
60
                                    onFailure: (err) => reject(err),
×
NEW
61
                                }),
×
NEW
62
                            );
×
NEW
63
                        }),
×
NEW
64
                ),
×
NEW
65
            )
×
NEW
66
                .then(() =>
×
NEW
67
                    displayAlert(
×
NEW
68
                        "Specifications were imported successfully.",
×
NEW
69
                        "success",
×
NEW
70
                    ),
×
NEW
71
                )
×
NEW
72
                .catch((err) =>
×
NEW
73
                    displayAlert(
×
NEW
74
                        `Unable to import specifications. Error is: ${
×
NEW
75
                            err?.message ?? err
×
NEW
76
                        }`,
×
NEW
77
                    ),
×
NEW
78
                );
×
NEW
79
        },
×
80
        [addSpecification, displayAlert],
42✔
81
    );
42✔
82

42✔
83
    const readFileContent = useCallback(
42✔
84
        (file: File) => {
42✔
NEW
85
            const fileReader = new FileReader();
×
NEW
86

×
NEW
87
            fileReader.onload = () => {
×
NEW
88
                try {
×
NEW
89
                    const specificationImport = JSON.parse(
×
NEW
90
                        fileReader.result as string,
×
NEW
91
                    );
×
NEW
92

×
NEW
93
                    validateSpecification(specificationImport)
×
NEW
94
                        .then(() => onValidateSuccessfully(specificationImport))
×
NEW
95
                        .catch((errors: string[]) => {
×
NEW
96
                            displayAlert(
×
NEW
97
                                <>
×
NEW
98
                                    <div>
×
NEW
99
                                        The imported file is not valid. Please
×
NEW
100
                                        check the following errors:
×
NEW
101
                                    </div>
×
NEW
102
                                    <div>
×
NEW
103
                                        {errors.map((error) => (
×
NEW
104
                                            <div key={error}>{error}</div>
×
NEW
105
                                        ))}
×
NEW
106
                                    </div>
×
NEW
107
                                </>,
×
NEW
108
                            );
×
NEW
109
                        });
×
NEW
110
                } catch (err) {
×
NEW
111
                    displayAlert("Unable to parse specification import.");
×
NEW
112
                }
×
NEW
113
            };
×
NEW
114
            fileReader.onerror = () =>
×
NEW
115
                displayAlert(
×
NEW
116
                    "Unable to import file. File content may not be readable.",
×
NEW
117
                );
×
NEW
118

×
NEW
119
            fileReader.readAsText(file);
×
NEW
120
        },
×
121
        [displayAlert, onValidateSuccessfully],
42✔
122
    );
42✔
123

42✔
124
    const onUploadFile = useCallback(
42✔
125
        (file: File | null) => {
42✔
NEW
126
            if (file) {
×
NEW
127
                readFileContent(file);
×
NEW
128
            }
×
NEW
129
        },
×
130
        [readFileContent],
42✔
131
    );
42✔
132

42✔
133
    return useMemo(
42✔
134
        () => ({
42✔
135
            specificationExport,
42✔
136
            specificationExportLink,
42✔
137
            onUploadFile,
42✔
138
        }),
42✔
139
        [specificationExport, specificationExportLink, onUploadFile],
42✔
140
    );
42✔
141
};
42✔
142

1✔
143
export default useSpecificationsTransfer;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc